Mailing List Archive

os.exec
Is there a way to send the output from a python-spawned external program
straight to the script without having to deal with the OS's piping and such?
I want to be able to say:

bob = get_output_from(os.execv('runme.exe', ('parm1', 'parm2')))

or something to that effect.

Thanks,

Jimmy

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
os.exec [ In reply to ]
jimmyth@my-dejanews.com writes:
> Is there a way to send the output from a python-spawned external program
> straight to the script without having to deal with the OS's piping and such?
> I want to be able to say:
>
> bob = get_output_from(os.execv('runme.exe', ('parm1', 'parm2')))
>
> or something to that effect.

There's commands.py, a library module:

Python 1.5.2 (#2, Apr 14 1999, 13:02:03) [GCC egcs-2.91.66 19990314 (egcs-1.1.2 on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import commands
>>> bob=commands.getoutput ('echo "bill"')
>>> print bob
bill
>>>

or there's popen:

Python 1.5.2 (#2, Apr 14 1999, 13:02:03) [GCC egcs-2.91.66 19990314 (egcs-1.1.2 on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import os
>>> bob=os.popen('echo "bill"').read()
>>> print bob
bill

>>>

which is pretty much what commands.py does under the hood. There's
also popen2:

Python 1.5.2 (#2, Apr 14 1999, 13:02:03) [GCC egcs-2.91.66 19990314 (egcs-1.1.2 on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import popen2
>>> o,i,e=popen2.popen3('echo "stdout"; echo "stderr" >& 2')
>>> o.read()
'stdout\012'
>>> e.read()
'stderr\012'
>>>

Or you can roll your own (I'd recommend starting with
popen2.py). These are more likely to do what you expect on unix-ish
OSes than windows, but I think the first two will work on windows too.


HTH
Michael