Avatar of kvolarich
kvolarich
Flag for United States of America asked on

How do I read through a directory of text files and find a line of data in each

I have a directory of text files (each has a unique name), I need to open and find the word "Error" in each one -- this may not always exist, but if it does, I need to create a log file that has the name of the text file read and that it returned an "Error".

I could also work with a script like this if it was written in Python.
Scripting LanguagesPythonWindows Batch

Avatar of undefined
Last Comment
HonorGod

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Shift-3

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
kvolarich

ASKER
How do you run this? From command line or can I put in a bat file?  I tried the bat file but nothing happened.  Also, the report.txt is saved where?  
kvolarich

ASKER
The script seems to work without the report.txt part.  Is the > correct in your snippet?  Should there be some other parameter qualifier?
kvolarich

ASKER
I just had to figure out how and where to save the report
Your help has saved me hundreds of hours of internet surfing.
fblack61
HonorGod

Do you want a python script to process
- an individual file?
- a list of files?
- a subset of files in a specific directory?
- something else?
kvolarich

ASKER
It was a list of files.  I also realize now that the output file needs to list the file name and also the word ERROR next to it.
HonorGod

Something like this perhaps?
import sys
import os.path
 
def FindText():
  argc = len( sys.argv )
  if argc < 3 :
    Usage()
  text  = sys.argv[ 1 ]
  files = sys.argv[ 2: ]
# print ' text: %s\nfiles: %s' % ( text, str( files ) )
  for filename in files :
    try :
      fh = open( filename )
      data = fh.read()
      fh.close()
      if data.find( text ) >= 0 :
        print filename, text
    except :
      sys.stderr.write( 'File not found: ' + filename + '\n' )
 
def Usage():
  print '''Command: FindText.py\n
   Role: To search text files for a specific text string\n
  Usage: python FindText.py textString fileName [fileName...]\n
Example: python FindText.py ERROR a.txt b.txt c.txt'''
  sys.exit()
 
if ( __name__ == '__main__' ) :
  FindText()
else :
  Usage()

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
kvolarich

ASKER
I don't see where I can setup where the file directory is?
kvolarich

ASKER
When I go to run it I can invalid syntax at the text = sys.argv[1]

it barks right before the word "text"
HonorGod

The filename parameters can include a complete path to the file.

If the filename contains a space (e.g., in the path), surround the filename in double quotes.

For example:

python FindText.py ERROR "C:\Program Files\My Files\A.txt" C:\Temp\B.txt

If a path is not included, then the file is expected to exist in the current directory.
So, you could do something like:

python C:\Utilities\FindText.py ERROR A.txt B.txt C.txt >output.log 2>error.log

Any files that don't exist will be listed in the "error.log" created by the preceding command.  Without specifying it, i.e.,

python C:\Utilities\FindText.py ERROR A.txt B.txt C.txt >output.log

The list of "File not found" messages would be seen on the screen, and the
list of files containing the specified text would be found in the specified output
file.

Does this make sense?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
HonorGod

What do you get when you type the following at the command prompt?

python -V
HonorGod

What operating system are you using?
kvolarich

ASKER
I'm using Python 2.4 and Windows XP
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
HonorGod

And you have the complete text of the script shown above?

Please paste, or attach what you actually have.

Thanks
kvolarich

ASKER
I don't want to have to list each file name, there are hundreds of them.  Can the script just look for all the .txt files listed in a specific directory?
kvolarich

ASKER
I'm not sure I was clear.  The word ERROR would be within the text file, not as part of the name of the file.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
HonorGod

Q: Can the script just look for all the .txt files listed in a specific directory?
A: Sure.  One moment please

>> I'm not sure I was clear.
>> The word ERROR would be within the text file, not as part of the name of the file.

Understood.  The script, as written allows the first parameter to specify the text to be found.  That way, if you wanted to use the same script to look for some other text, all you would need to do would be to execute the script, and pass it the text to be found.

Let me revise the script to not require filenames, or the text...
HonorGod

What parameters would you like to specify?

The directory to be searched?

Anything?
kvolarich

ASKER
The directory to be search and the text word within the file to look for
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
HonorGod

Something like this?

All you would need to do is:

- open a command prompt
- "cd" to the directory containing the .txt files to be searched
- execute the command, something like:

  python "C:\Utilities\FindText.py" >output.log


import sys
import os
 
def FindText():
  files = os.listdir( os.getcwd() )
  files.sort()
# print ' text: %s\nfiles: %s' % ( text, str( files ) )
  for filename in files :
    if filename[ -4: ] == '.txt' :
      try :
        f = open( filename )
        data = f.read()
        f.close()
        if data.find( 'ERROR' ) >= 0 :
          print filename, 'ERROR'
      except :
        pass
 
if ( __name__ == '__main__' ) :
  FindText()

Open in new window

HonorGod

oh, you've already closed the question.

nevermind.
HonorGod

please open another (perhaps related) question.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes