fastest way to test file for string?
Scott David Daniels
Scott.Daniels at Acm.Org
Fri Jun 5 15:20:37 EDT 2009
Tim Chase wrote:
>> Hi. I need to implement, within a Python script, [functionality like]:
>> grep -rl some_string some_directory
>
> 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):
Note order wrong here
> ...
>
> for filename in find_files_containing(
> "/path/to/wherever/",
> "some_string"
> ):
> print filename
I like results in a nice order, so I do something more like:
def find_files_containing(base_dir, string_to_find):
for path, dirs, files in os.walk(base_dir): # note order
for fname in sorted(files): # often endswith in here
full_name = os.path.join(path, fname)
try:
with open(full_name) as f:
for line in f:
if string_to_find in line:
yield full_name
break
except IOError, why:
print ("On %s in %s: %s' % (fname, path, why))
# usually several subdirs to avoid
dirs[:] = sorted([d for d in dirs
if d[0] != '.' and d not in ('RCS', 'CVS')])
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list