how to invoke the shell command and then get the result in python
Fredrik Lundh
fredrik at pythonware.com
Tue Dec 5 02:28:45 EST 2006
Bin Chen wrote:
> I want to do following: get a user input regex, then pass this as a
> parameter to grep, and then get the result from grep.
>
> Any code snip to implement the similar function? I am a python newbie.
import os
for line in os.popen("grep pattern *.txt"):
print line,
also see os.system and subprocess.
note that if you want to write portable code, you can implement your own
"grep" using the "re" module:
import re
p = re.compile(pattern)
for index, line in enumerate(open(filename)):
if p.match(line):
print index, line,
</F>
More information about the Python-list
mailing list