Python Forum
How to make a script to find a certain word in text files in a whole directory ?
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make a script to find a certain word in text files in a whole directory ?
#2
I have created 2 different .txt files in the directory of the script called test.txt and test2.txt
and i have written word in each file but different lines. and got the following result after executing the script:

<_io.TextIOWrapper name=[b]'test.txt'[/b] mode='r' encoding='UTF-8'>
<_io.TextIOWrapper name=[b]'test2.txt'[/b] mode='r' encoding='UTF-8'>
I have used the following libs:
from os import listdir
from os.path import isfile, join

What I did was to create a list called db = [], then I have used listdir('.') to iterate trough each item in the current directory.
after that by using endswith method if dir_.endswith('.txt'): i have been able to only read the txt files.
and then last part was to read each file and see if it was containing the word word.
with open(dir_,'r') as file_:
Reply


Messages In This Thread
RE: How to make a script to find a certain word in text files in a whole directory ? - by idontreallywolf - Apr-27-2017, 09:54 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Filer and sort files by modification time in a directory tester_V 6 244 May-02-2024, 05:39 PM
Last Post: tester_V
  Loop through all files in a directory? Winfried 10 494 Apr-23-2024, 07:38 PM
Last Post: FortuneCoins
  Make entire script run again every 45 mo NDillard 0 336 Jan-23-2024, 09:40 PM
Last Post: NDillard
  uploading files from a ubuntu local directory to Minio storage container dchilambo 0 494 Dec-22-2023, 07:17 AM
Last Post: dchilambo
  Script that alternates between 2 text messages DiscoMatic 1 548 Dec-12-2023, 03:02 PM
Last Post: buran
  Trying to make a board with turtle, nothing happens when running script Quascia 3 711 Nov-01-2023, 03:11 PM
Last Post: deanhystad
  Replace a text/word in docx file using Python Devan 4 3,607 Oct-17-2023, 06:03 PM
Last Post: Devan
  Python script that deletes symbols in plain text nzcan 3 715 Sep-05-2023, 04:03 PM
Last Post: deanhystad
  change directory of save of python files akbarza 3 940 Jul-23-2023, 08:30 AM
Last Post: Gribouillis
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,486 Jun-27-2023, 01:17 PM
Last Post: diver999

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020