By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

os.popen does not seem to catch stdout

P: n/a
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.

Nov 7 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Nov 7, 7:58 am, zane.selv...@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/chi...er/000141.html
http://mail.python.org/pipermail/pyt...er/347508.html

This seems to be a recipe on it:

http://aspn.activestate.com/ASPN/Coo.../Recipe/440554

Mike

Nov 7 '07 #2

P: n/a
On Nov 7, 11:31 am, kyoso...@gmail.com wrote:
On Nov 7, 7:58 am, zane.selv...@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)

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/chi...er/347508.html

This seems to be a recipe on it:

http://aspn.activestate.com/ASPN/Coo.../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

Nov 7 '07 #3

P: n/a
On Nov 7, 12:17 pm, "gregpin...@gmail.com" <gregpin...@gmail.com>
wrote:
On Nov 7, 11:31 am, kyoso...@gmail.com wrote:
On Nov 7, 7:58 am, zane.selv...@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)
Seehttp://docs.python.org/lib/node529.htmlformore details.
You'll also find a fairly interesting thread on this topic here:
http://mail.python.org/pipermail/chi...000141.htmlhtt...
This seems to be a recipe on it:
http://aspn.activestate.com/ASPN/Coo.../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
I've never had to put the command into a list or tuple...but you're
welcome to try it that way.

Mike

Nov 7 '07 #4

P: n/a
On Wed, 07 Nov 2007 18:35:51 +0000, kyosohma wrote:
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
Nov 7 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.