This is actually not a good way to use Python, but if you need to do something like this, do it right:
from __future__ import with_statement import subprocess def grep_lines(filename, query_filename): with open(query_filename, "rb") as myfile: for line in myfile: subprocess.call(["/bin/grep", line.strip(), filename]) grep_lines("my2.txt", "query.txt")
And I hope that your file does not contain characters that have special meanings in regular expressions =)
Alternatively, you can only do this with grep :
grep -f query.txt my2.txt
It works as follows:
~ $ cat my2.txt One two two two two three ~ $ cat query.txt two two three ~ $ python bar.py two two two three
source share