Running shell programs from Python

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


In article <38FC64DA.7D297563 at ffi.no>, seh at ffi.no 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 = p.read()

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")
  p.close()

(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

   <http://www.python.org/doc/current/lib/lib.html>

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



More information about the Python-list mailing list