Running shell programs from Python

Grant Edwards grant at nowhere.
Tue Apr 18 12:12:53 EDT 2000

In article <38FC64DA.7D297563 at>, seh at wrote:
>I have just started with Python and have some questions.. I have some
>compiled c programs that I normally run from the shell. I can use | to
>pipe the output from one as input to the next. I would like to call the
>programs from Python. Could someone please indicate how this is done?
>Any references?

If you just want to run the programs you can use:

  os.system("foo | bar")

If you want to capture the output of the pipeline:

  p = os.popen("foo | bar","r")
  outputString =

If you want to write data to the pipeline:

  p = os.popen("foo | bar","w")
  p.write("data to be sent to foo\n")
  p.write("more data to foo\n")

(Remember to do an "import os" if you want to do any of the above)

If you want to both write data to the pipeline and capture it's
output, use the popen2 module, examples of which can be found
in the on-line docs


Grant Edwards                   grante             Yow!  I'm protected by
                                  at               a ROLL-ON I rented from

More information about the Python-list mailing list