473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 4869
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tung Wai Yip | last post by:
I've build the following utility. It works so far but I want to make sure I'm doing the right thing. I am running Python 2.3 on windows 2000. def execute(cmd): """ execute cmd in sub-process,...
5
by: damacy | last post by:
hello, everyone. i am trying to write a program which executes SQL commands stored in ..sql files. i wrote a function called psql() whose contents look like the following. .......
9
by: Clodoaldo Pinto Neto | last post by:
Output from the shell: $ set | grep IFS IFS=$' \t\n' Output from subprocess.Popen(): "IFS=' \t\n" Both outputs for comparison:
2
by: Greg Ercolano | last post by:
When I use os.popen(cmd,'w'), I find that under windows, the stdout of the child process disappears, instead of appearing in the DOS window the script is invoked from. eg: C:\type foo.py import...
3
by: Jesse | last post by:
Hi all, I have a problem using wget and Popen. I hope someone can help. -- Problem -- I want to use the command: wget -nv -O "dir/cpan.txt" "http://search.cpan.org" and capture all it's...
8
by: dmoore | last post by:
Hi folks, I've seen the following issue come up in multiple posts to this mailing list: I have a python program that spawns a child process with popen or popen2 or popen3 or popen2.popen2...
12
by: Eric_Dexter | last post by:
I am trying to modify a programming example and I am coming up with two problems... first is that I can't seem to pass along the arguments to the external command (I have been able to do that with...
8
by: clyfish | last post by:
In cmd, I can use find like this. C:\>netstat -an | find "445" TCP 0.0.0.0:445 0.0.0.0:0 LISTENING UDP 0.0.0.0:445 *:* C:\> And os.system is OK....
5
by: thedsadude | last post by:
Hello, I'm launching a script as follows: <code> p = subprocess.Popen() p.wait() </code> If p.py writes to sys.stdout, then it is shown on the console.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.