Hi. I need to implement, within a Python script, the same
functionality as that of Unix's
grep -rl some_string some_directory
I.e. find all the files under some_directory that contain the string
"some_string".
I'd do something like this untested function:
def find_files_containing(base_dir, string_to_find):
for path, files, dirs in os.walk(base_dir):
for fname in files:
full_name = os.path.join(path, fname)
f = file(full_name)
for line in f:
if string_to_find in line:
f.close()
yield full_name
break
else:
f.close()
for filename in find_files_containing(
"/path/to/wherever/",
"some_string"
):
print filename
It's not very gracious regarding binary files, but caveat coder.
-tkc
--
http://mail.python.org/mailman/listinfo/python-list