[Tutor] calling other process?
Alan Gauld
alan.gauld at btinternet.com
Mon Feb 9 01:52:40 CET 2009
"Bernard Rankin" <berankin99 at yahoo.com> wrote
> http://docs.python.org/library/subprocess.html
>
> Is:
>
> from subprocess import Popen
> output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0]
>
> Really the replacement for:
>
> output = `mycmd myarg`
Yes, because it works more consistently and reliably across
operating systems for one reason. Its also much more powerful
and flexible.
> Is there a less verbose, yet still official supported, way to
> capture STDOUT from a different program?
os.popen and the commands module still work, they are
just deprecated. It is better to get used to subprocess
although it is more verbose. But at least it can do all the things
you need so you don't need to juggle between backticks,
popen(all 4 versions of it!), spawn, system,commands, etc...
Remember that in the zen of Python :
a) There should be one-- and preferably only one --obvious way to do
it.
b) explicit is better than implicit.
http://www.python.org/dev/peps/pep-0020/
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list