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:

result = popen2(command)[0].read()

It only prints the results to the console and never returns a result to the variable. Whats going on?

-Alec Hussey

9 Years
Discussion Span
Last Post by maddog39

It sounds to me like your child process is misbehaving. May I ask which command you are executing?


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...)



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

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

I just had a thought. Is your C program compiling correctly or is it producing errors? You might have to use popen3()...


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.

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

Now it works! Thanks alot!

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.