This is a simple script that will search text file recursively for words in a list. Then it will print the file name (including the full path), file creation/modification date, the actual line of text and line number. Download the source here.
import os, sys, datetime, time def wordSearch(): # These is variables temporary until the GUI is completed path = 'aDirectory' words = ['some','words','go','here' ] # Set variables fn = '' filelist = [] # List recursive directory content for root, subFolders, files in os.walk(path): # List each item in the recursive loop for file in files: infile = os.path.join(root,file) # If the item is a file, read files line by line and search for key # words, else pass the item try: # Open file for reading f = open(infile, 'r') # Set line number counter lc = 0 # Read the file line by line for line in f.readlines(): # Count the line number lc = lc + 1 # Check to see if the line contains any words in the words list for item in words: # If the item in words list is not present pass and move # to next item if not item in line: pass else: # If a match is found print the full path of the file, # the line number it occurred on, the line of the # occurance and it's name and the creation and # modification date. # If the file name is the same just print the line # number of the occurance and the line of the occurance if (fn != infile): ct = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getctime(infile))) mt = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getmtime(infile))) filelist.append(infile) print infile + '\nFile Created On : ' + str(ct) + '\nFile Modified On : ' + str(mt) + '\n Line ' + str(lc) + '\n ' + line fn = infile else: #pass print ' Line ' + str(lc) + '\n ' + line f.close() except: pass print filelist wordSearch()