Capture stdin output from popen2

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 39
Reputation: maddog39 is an unknown quantity at this point 
Solved Threads: 5
maddog39 maddog39 is offline Offline
Light Poster

Capture stdin output from popen2

 
0
  #1
Nov 3rd, 2007
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:
  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Capture stdin output from popen2

 
0
  #2
Nov 3rd, 2007
It sounds to me like your child process is misbehaving. May I ask which command you are executing?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 39
Reputation: maddog39 is an unknown quantity at this point 
Solved Threads: 5
maddog39 maddog39 is offline Offline
Light Poster

Re: Capture stdin output from popen2

 
0
  #3
Nov 3rd, 2007
Im executing gcc to compile a file in this instance.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Capture stdin output from popen2

 
0
  #4
Nov 3rd, 2007
On linux? Or windows?
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Capture stdin output from popen2

 
0
  #5
Nov 3rd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 39
Reputation: maddog39 is an unknown quantity at this point 
Solved Threads: 5
maddog39 maddog39 is offline Offline
Light Poster

Re: Capture stdin output from popen2

 
0
  #6
Nov 3rd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Capture stdin output from popen2

 
0
  #7
Nov 3rd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 39
Reputation: maddog39 is an unknown quantity at this point 
Solved Threads: 5
maddog39 maddog39 is offline Offline
Light Poster

Re: Capture stdin output from popen2

 
0
  #8
Nov 3rd, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3087 | Replies: 7
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC