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
  Make entire script run again every 45 mo NDillard 0 316 Jan-23-2024, 09:40 PM
Last Post: NDillard
  uploading files from a ubuntu local directory to Minio storage container dchilambo 0 447 Dec-22-2023, 07:17 AM
Last Post: dchilambo
  Script that alternates between 2 text messages DiscoMatic 1 515 Dec-12-2023, 03:02 PM
Last Post: buran
  Trying to make a board with turtle, nothing happens when running script Quascia 3 657 Nov-01-2023, 03:11 PM
Last Post: deanhystad
  Replace a text/word in docx file using Python Devan 4 3,293 Oct-17-2023, 06:03 PM
Last Post: Devan
  Python script that deletes symbols in plain text nzcan 3 687 Sep-05-2023, 04:03 PM
Last Post: deanhystad
  change directory of save of python files akbarza 3 875 Jul-23-2023, 08:30 AM
Last Post: Gribouillis
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,303 Jun-27-2023, 01:17 PM
Last Post: diver999
Question Need help for a python script to extract information from a list of files lephunghien 6 1,076 Jun-12-2023, 05:40 PM
Last Post: snippsat
  script to calculate data in csv-files ledgreve 0 1,092 May-19-2023, 07:24 AM
Last Post: ledgreve

Forum Jump:

User Panel Messages

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