63 lines
1.7 KiB
Plaintext
63 lines
1.7 KiB
Plaintext
From: mwh21 at cam.ac.uk (Michael Hudson)
|
|
Date: 26 Apr 1999 13:43:18 +0100
|
|
Subject: os.exec
|
|
References: <7g1jv4$5eh$1@nnrp1.dejanews.com>
|
|
Message-ID: <m3wvyzd02h.fsf@atrus.jesus.cam.ac.uk>
|
|
Content-Length: 1438
|
|
X-UID: 885
|
|
|
|
jimmyth at 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
|
|
|
|
|
|
|
|
|