943,985 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 4398
  • Python RSS
Nov 3rd, 2007
0

Capture stdin output from popen2

Expand Post »
Hello all,

In my python application I am trying to capture the output of stdin from a command that I run via popen2(). The python docs say that it returns a tuple of (stdin, stdout) however and i run:
python Syntax (Toggle Plain Text)
  1. result = popen2(command)[0].read()
It only prints the results to the console and never returns a result to the variable. Whats going on?

Thanks!
-Alec Hussey
Similar Threads
Reputation Points: 10
Solved Threads: 5
Light Poster
maddog39 is offline Offline
39 posts
since Oct 2007
Nov 3rd, 2007
0

Re: Capture stdin output from popen2

It sounds to me like your child process is misbehaving. May I ask which command you are executing?
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 3rd, 2007
0

Re: Capture stdin output from popen2

Im executing gcc to compile a file in this instance.
Reputation Points: 10
Solved Threads: 5
Light Poster
maddog39 is offline Offline
39 posts
since Oct 2007
Nov 3rd, 2007
0

Re: Capture stdin output from popen2

On linux? Or windows?
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 3rd, 2007
0

Re: Capture stdin output from popen2

Well, first off there are two versions of popen2(). One is os.popen2(command), and it returns (child_stdin, child_stdout).

The other is popen2.popen2(command), and perversely, it returns (child_stdout, child_stdin).

So make sure that you don't have the two reversed.

(Who made that decision, anyways?! Grr...)

Jeff
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Nov 3rd, 2007
0

Re: Capture stdin output from popen2

I am on linux, and I am using the popen2 module.
http://docs.python.org/lib/module-popen2.html

[Edit]
Thats my mistake then isn't it? I am reading from the first element of the tuple, when I should be reading from the second one.
Last edited by maddog39; Nov 3rd, 2007 at 9:58 pm.
Reputation Points: 10
Solved Threads: 5
Light Poster
maddog39 is offline Offline
39 posts
since Oct 2007
Nov 3rd, 2007
0

Re: Capture stdin output from popen2

No, you had that correct. The return tuple is (child's output, child's input).

I don't know what's wrong...


[EDIT]
I just had a thought. Is your C program compiling correctly or is it producing errors? You might have to use popen3()...
Last edited by Duoas; Nov 3rd, 2007 at 10:01 pm.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 3rd, 2007
0

Re: Capture stdin output from popen2

Yea, aaah. I intentionally put syntax errors in there to test it. But gcc is most likely printing those errors to stderr and thats why its not capturing them. Alright, I'll try it with popen3.

[Edit]
Actually, popen4() would probably work better, but whatever works I guess.

[Edit]
Now it works! Thanks alot!
Last edited by maddog39; Nov 3rd, 2007 at 10:18 pm.
Reputation Points: 10
Solved Threads: 5
Light Poster
maddog39 is offline Offline
39 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: def save(): print "HOW DO I DO THIS?"
Next Thread in Python Forum Timeline: noob: What and when you would use Decimal vs Float Type





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC