os.popen does not seem to catch stdout

kyosohma at gmail.com kyosohma at gmail.com
Wed Nov 7 11:31:08 EST 2007


On Nov 7, 7:58 am, zane.selv... at gmail.com wrote:
> Hi there,
>
> I've been banging my head against this for a day, and I can't take it
> anymore.  It's probably a stupid error, but I don't see where.
>
> I'm trying to use Python to call an external program, and then catch
> and process the output of that program.  Seems simple enough.  The
> command I'm trying to run, in the test, is:
>
> "/Users/zane/svn/stress/satstress/satstress -r1.561e+06 -a5 -s45000 -
> e0 -R6.709e+08 -g1.31472 -m1.8987e+27 -Q57100.1 -n0.333355 -Y9.29881e
> +09 -k1e+22 -Z1.19173 -z-6.293e-05 -V0.309434 -v-2.903e-05 -W1.81305 -
> w-0.00418645 -U0.474847 -u-0.00276624 -C /tmp/18_tmp.gen -b 60"
>
> When I run it at my zsh prompt, I get the expected output.
>
> If I let ss_cmd equal the above string within ipython (or the standard
> python interactive interpreter):
>
> ss_outlines = os.popen(ss_cmd).readlines()
>
> ss_outlines contains the same output I saw when I ran the command at
> my zsh prompt, one line per list element, as expected.
>
> However, when I try doing the same thing from within a program, it
> fails.  ss_outlines is an empty list.
>
> I've tried using subprocess.Popen(), and subprocess.call(), and
> subprocess.check_call(), and all have yielded similar results.  I did
> find, however, that the return value python is getting from the
> program I'm calling is different from what I get at the command line
> (I get 0, python gets -11).
>
> Does this ring a bell for anyone?
>
> I'm using Python 2.5.1 on a Mac running OS X 10.5.

I think when you use subprocess.Popen, you need to do something set
the shell to True to get it to behave like running from a command
prompt:

subprocess.Popen('some command', shell=True)

See http://docs.python.org/lib/node529.html for more details.

You'll also find a fairly interesting thread on this topic here:

http://mail.python.org/pipermail/chicago/2005-November/000141.html
http://mail.python.org/pipermail/python-list/2005-October/347508.html

This seems to be a recipe on it:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/440554

Mike




More information about the Python-list mailing list