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

Recommended Answers

All 7 Replies

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

Im executing gcc to compile a file in this instance.

On linux? Or windows?

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.