Running shell programs from Python

Benyang Tang btang at pacific.jpl.nasa.gov
Tue Apr 18 17:33:59 CEST 2000


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?
> 
> Regards
> Svein-Erik Hamran

Use os.popen() or popen2.popen2(). 

Reference:
http://www.python.org/doc/current/lib/os-newstreams.html
http://www.python.org/doc/current/lib/module-popen2.html

Here is an example (for Unix):

import os,popen2
str1 = os.popen('ls','r').read()
print str1
out1,in1 = popen2.popen2('cat')
in1.write(str1)
in1.close()
str2 = out1.read()
out1.close()
print str2

===============
Benyang Tang



More information about the Python-list mailing list