This is inspired for recent poster, who asked to check multiple strings in multiple files.
import os def process_file((extensions, find), path, filelist): correct = (os.path.join(path,filename) for filename in filelist if not extensions or any(filename.endswith(ext) for ext in extensions)) for this in correct: try: print('%60s has %s in it.' % (this, next(tofind for tofind in find if tofind in open(this, 'rb').read()) )) except StopIteration: pass def files_recursively(topdir, extensions, find): os.path.walk(os.path.realpath(topdir), process_file, (extensions, find)) files_recursively('d:/test',('.py','.txt'), ('Tkinter','itertools','copyright'))
About the Author
IT Pro doing Eng-Fin-Eng translations