The small piece of code take the folder
name and the pattern of the possible file names which might have the
text then it takes again the pattern/actual text which one is being
searched.
It prints the file name and line
searching out the folder.
import os import re #import sys #shutil def matchline(line,match): #cmatch=re.compile(match) cmatch=match checkmatch=cmatch.search(line) if checkmatch!=None: print "-----------------------------------------" print "Got the line...."+line return 0 else: return -1 def readfilestring(filename, match): linestring = open(filename, 'r') l=linestring.readline() while l != '': #print l if matchline(l,match) ==0: print "<----in file" + filename print "-----------------------------------------" l=linestring.readline() def search_file(search_path,filename,stringpatt): try: dirList = os.listdir(search_path) except Exception, e: print "Search error for OS issue - " print e else: for d in dirList: if os.path.isdir(search_path+os.sep+d) == True: search_file(search_path+os.sep+d,filename,stringpatt) elif os.path.isfile(search_path+os.sep+d) == True: #print d + " is a file update whatever required" pmatch=filename.search(d) if pmatch==None: ss=1 else: #print "-----------------------------------------" #print "The file is found - "+search_path+os.sep+d #print "-----------------------------------------" readfilestring(search_path+os.sep+d, stringpatt) else: print "Unknown filesystem object - "+search_path+os.sep+d if __name__ == '__main__': search_path = raw_input("Enter search_path: ") filepattern = raw_input("Enter filepattern: ") stringpattern= raw_input("Enter stringpattern: ") try: retest="filepattern" ire=re.compile(filepattern,re.I) retest="stringpattern" strre=re.compile(stringpattern,re.I) except Exception, e: print "Reg Ex problem - for "+retest print e else: if os.path.exists(search_path): find_file = search_file(search_path,ire,strre) else: print "Not a valid path - "+search_path
Amazing post. Keep it up. python training in Chennai