how to invoke the shell command and then get the result in python

petercable at gmail.com petercable at gmail.com
Tue Dec 5 03:22:25 EST 2006



Fredrik Lundh  wrote:

>         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:

> </F>

Also, for a wrapper around popen, try commands:

  import commands

  pattern = raw_input('pattern to search? ')
  print commands.getoutput('grep %s *.txt' % pattern)

Pete




More information about the Python-list mailing list