This is inspired for recent poster, who asked to check multiple strings in multiple files.
scan filetree for files containing text
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'))
TrustyTony 888 ex-Moderator Team Colleague Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.