User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 456,580 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,582 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 1653 | Replies: 7
Reply
Join Date: Oct 2007
Location: Pennsylvania, US
Posts: 37
Reputation: maddog39 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 3
maddog39 maddog39 is offline Offline
Light Poster

Capture stdin output from popen2

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Capture stdin output from popen2

  #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  
Join Date: Oct 2007
Location: Pennsylvania, US
Posts: 37
Reputation: maddog39 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 3
maddog39 maddog39 is offline Offline
Light Poster

Re: Capture stdin output from popen2

  #3  
Nov 3rd, 2007
Im executing gcc to compile a file in this instance.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Capture stdin output from popen2

  #4  
Nov 3rd, 2007
On linux? Or windows?
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: Capture stdin output from popen2

  #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  
Join Date: Oct 2007
Location: Pennsylvania, US
Posts: 37
Reputation: maddog39 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 3
maddog39 maddog39 is offline Offline
Light Poster

Re: Capture stdin output from popen2

  #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  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Capture stdin output from popen2

  #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  
Join Date: Oct 2007
Location: Pennsylvania, US
Posts: 37
Reputation: maddog39 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 3
maddog39 maddog39 is offline Offline
Light Poster

Re: Capture stdin output from popen2

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 6:23 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC