[FEEDBACK] Is this script efficient...is there a better way?

Bob X bobx at linuxmail.org
Wed Sep 11 23:01:41 CEST 2002

I am a newbie at this and this is a script I have come up with. I am 
looking for pointers on any "newbie" gotchas or ways to do it better.

I use this to parse a log file (30MB+) for keywords and write the lines 
that are found into another file.

Any thoughts would be appreciated...  :-)


#!/usr/local/bin/python -w

# import the needed libs
import sys, string

# make sure the command line arguments are there
if len(sys.argv) < 3:
     print "usage: fread.py [log file] [hit file]"

# open the files with some error checking
     inFile = open(sys.argv[1],"r")
except IOError:
     print "Cannot open log file!\n"

     outFile = open(sys.argv[2],"w")
except IOError:
     print "Cannot open hits file!\n"

# build list of keywords
kw = [ "some", "words" ]

# loop through the list and print the lines to a file
for line in inFile.readlines():
     for badword in kw:
         if line.find(badword) > -1:
             result = '%s %s' % (badword, line)
             print result            # Print the result
             outFile.write(result)   # Write the result

# close the files

# let me know when it's done
print "Finished processing file..."

More information about the Python-list mailing list