How should I use grep from python?
Tim Chase
python.list at tim.thechases.com
Thu May 7 09:25:52 EDT 2009
> I'm writing a command-line application and I want to search through lots
> of text files for a string. Instead of writing the python code to do
> this, I want to use grep.
>
> This is the command I want to run:
>
> $ grep -l foo dir
>
> In other words, I want to list all files in the directory dir that
> contain the string "foo".
>
> I'm looking for the "one obvious way to do it" and instead I found no
> consensus. I could os.popen, commands.getstatusoutput, the subprocess
> module, backticks, etc.
While it doesn't use grep or external processes, I'd just do it
in pure Python:
def files_containing(location, search_term):
for fname in os.listdir(location):
fullpath = os.path.join(location, fname)
if os.isfile(fullpath):
for line in file(fullpath):
if search_term in line:
yield fname
break
for fname in files_containing('/tmp', 'term'):
print fname
It's fairly readable, you can easily tweak the search methods
(case sensitive, etc), change it to be recursive by using
os.walk() instead of listdir(), it's cross-platform, and doesn't
require the overhead of an external process (along with the
"which call do I use to spawn the function" questions that come
with it :)
However, to answer your original question, I'd use os.popen which
is the one I see suggested most frequently.
-tkc
More information about the Python-list
mailing list