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 ?
#1
I have not yet attempted to make this.
i browsed for 1 hour on web,could not find a single article


Wall

>Thanks in advance Pray
Reply
#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
#3
You don't have to search for 'how to find a single word in a bunch of files using Python'.
Do it step by step.
How to get a list of files in a given directory. click
How to open and read from a file. click
How to find a word in a string. click

Then you need to loop over each file in the list of the found files, open it, read it line by line, check for the word in each line. Print out the file name, and the line number where the word is present.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to make it so whatever I input into a script gets outputted on a different file spermatozwario 4 1,096 Nov-24-2024, 12:58 PM
Last Post: deanhystad
  deleting files in program files directory RRADC 6 2,868 Aug-21-2024, 06:11 PM
Last Post: snippsat
  Trying to generating multiple json files using python script dzgn989 4 2,113 May-10-2024, 03:09 PM
Last Post: deanhystad
  Filer and sort files by modification time in a directory tester_V 5 2,246 May-02-2024, 05:39 PM
Last Post: tester_V
  Loop through all files in a directory? Winfried 10 4,031 Apr-23-2024, 07:38 PM
Last Post: FortuneCoins
  Make entire script run again every 45 mo NDillard 0 880 Jan-23-2024, 09:40 PM
Last Post: NDillard
  uploading files from a ubuntu local directory to Minio storage container dchilambo 0 1,308 Dec-22-2023, 07:17 AM
Last Post: dchilambo
  Script that alternates between 2 text messages DiscoMatic 1 1,250 Dec-12-2023, 03:02 PM
Last Post: buran
  Trying to make a board with turtle, nothing happens when running script Quascia 3 1,693 Nov-01-2023, 03:11 PM
Last Post: deanhystad
  Replace a text/word in docx file using Python Devan 4 20,034 Oct-17-2023, 06:03 PM
Last Post: Devan

Forum Jump:

User Panel Messages

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