5
votes

Apologies for another question about Python subprocesses, but I couldn't find an answer to this one.

I am having trouble with some Python code which calls a subprocess on Windows 7 64-bit. When the subprocess's stdout is sent to a pipe, no output is produced. The subprocess appears to run and terminate without problems, it just doesn't produce any output.

EDIT: The same code works correctly on WinXP 32bit, so I updated the question title.

# (listing 1)
from subprocess import *
#cmdline= (a valid command line)
proc = Popen(cmdline,shell=True,stdout=PIPE,stderr=PIPE)
out, err = proc.communicate()
print( out )
print( err )

This gives output

out:

err:

However, when the subprocess's output is not piped, it produces output as expected:

# (listing 2)
proc = Popen(cmdline,shell=True)
proc.communicate()

This gives the expected output to console.

I'm confident the executable is actually writing its output to stdout. I have the C source code, and I added the line:

fprintf(stdout, "HELLO");

Again, "HELLO" is seen when running listing 2, but not listing 1.

I also tried making a new C++ executable and calling that from cmdline:

#include <iostream>

int main()
{
    std::cout << "HELLO" << std::endl;
}

The same thing still happens - "HELLO" is seen when running listing 2, but not listing 1.

If I set cmdline to 'dir', the expected thing happens for both listing 1 and listing 2 - directory contents are printed to the console.

Other things I tried: Python 3.3 and Python 2.7 (same results); bufsize=0 (same results); checking proc.returncode (it's 0, as expected); removing stderr=PIPE (in which case listing 1 gives "err: None" as expected).

EDIT - I also tried out = proc.stdout instead of the communicate() method with the same results. Python documentation and other questions suggest the communicate() method is the right one to use.

1
how do you run your script? What is cmdline? - jfs
Thanks. I already tried doing readline() on proc.stdout instead of using communicate() but this still doesn't produce any output. The Python documentation and other questions on stackoverflow suggest the communicate() method is preferred, so I wrote it that way in my question. I edited the question to add this information. Command line for running the script is just C:\Python27\python.exe myscript.py. - Ed Earl
So check_output, check_call, communicate (exactly as shown in my answer), winpexpect, etc don't work in your case? - jfs
check_output, check_call and communicate as shown in your answer all have the same result, which makes sense, since they all wrap Popen. I will try winpexpect but I still want to find out why Popen doesn't work for me. - Ed Earl

1 Answers

2
votes

It's not a Python problem at all. My firewall settings had "sandboxed" the executables, and this caused their output to be discarded without any kind of warning or error.

It would have made more sense to prevent them from executing. I think I need to use different firewall software.