471,348 Members | 1,894 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

os.popen does not seem to catch stdout

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
4 4739
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
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
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
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.

Similar topics

5 posts views Thread by damacy | last post: by
9 posts views Thread by Clodoaldo Pinto Neto | last post: by
2 posts views Thread by Greg Ercolano | last post: by
3 posts views Thread by Jesse | last post: by
12 posts views Thread by Eric_Dexter | last post: by
5 posts views Thread by thedsadude | last post: by
1 post views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.