os.popen does not seem to catch stdout

Z

zane.selvans

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.
 
K

kyosohma

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
 
G

gregpinero

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)

Seehttp://docs.python.org/lib/node529.htmlfor more details.

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

http://mail.python.org/pipermail/ch...ipermail/python-list/2005-October/347508.html

This seems to be a recipe on it:

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

Mike


That's right, and I think you also want to put the whole command in a
list or tuple, one item for each arg. I don't know why though.

-Greg
 
K

kyosohma

That's right, and I think you also want to put the whole command in a
list or tuple, one item for each arg. I don't know why though.

-Greg

I've never had to put the command into a list or tuple...but you're
welcome to try it that way.

Mike
 
M

Marc 'BlackJack' Rintsch

I've never had to put the command into a list or tuple...but you're
welcome to try it that way.

You don't *have* to but if you do the module takes care of quoting the
arguments if necessary.

Ciao,
Marc 'BlackJack' Rintsch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top