How should I use grep from python?

Matthew Wilson matt at tplus1.com
Thu May 7 09:46:13 EDT 2009


On Thu 07 May 2009 09:25:52 AM EDT, Tim Chase wrote:
> While it doesn't use grep or external processes, I'd just do it 
> in pure Python:

Thanks for the code!

I'm reluctant to take that approach for a few reasons:

1. Writing tests for that code seems like a fairly large amount of work.
I think I'd need to to either mock out lots of stuff or create a bunch
of temporary directories and files for each test run.

I don't intend to test that grep works like it says it does.  I'll
just test that my code calls a mocked-out grep with the right options
and arguments, and that my code behaves nicely when my mocked-out
grep returns errors.

2. grep is crazy fast.  For a search through just a few files, I doubt
it would matter, but when searching through a thousand files (which is
likely) I suspect that an all-python approach might lag behind.  I'm
speculating here, though.

3. grep has lots and lots of cute options.  I don't want to think about
implementing stuff like --color, for example.  If I just pass all the
heavy lifting to grep, I'm already done.

On the other hand, your solution is platform-independent and has no
dependencies.  Mine depends on an external grep command.

Thanks again for the feedback!

Matt




More information about the Python-list mailing list