Obtaining keyword for matching string in Python -
i writing script searches through text files in directory words. if "true" returned, print text file found in. there way return/print keyword found? thank you!
from glob import glob def main(): directory = "c:\\files folder\\*.txt" filelist = glob(directory) keywords = ("hello", "goodbye", "fake") textfile in filelist: f = open(textfile, 'r') line in f: if any(s in line s in keywords): print "keyword found!", f.name if __name__ == '__main__': main()
for line in f: s in keywords: if s in line: print 'key word found!', f.name, s
Comments
Post a Comment