5

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.

Ed Earl
  • 71
  • 1
  • 4
  • 1
    try some examples from [How do I get all of the output from my .exe using subprocess and Popen?](http://stackoverflow.com/a/12606327/4279) – jfs Oct 06 '12 at 11:59
  • how do you run your script? What is cmdline? – jfs Oct 06 '12 at 12:03
  • 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 Oct 06 '12 at 12:38
  • 1
    So check_output, check_call, communicate (exactly as shown in my answer), winpexpect, etc don't work in your case? – jfs Oct 06 '12 at 12:43
  • 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 Oct 06 '12 at 13:07
  • to be clear `repr(output) == ''` if you call `output = check_output(r'c:\path\to\hello.exe')` and run the script as `C:\Python27\python.exe hello.py` from console. Is it correct? (where `hello.exe` produces 'HELLO' otherwise) – jfs Oct 06 '12 at 13:16
  • I found the problem. Thanks for your help! – Ed Earl Oct 06 '12 at 13:32

1 Answers1

2

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.

Ed Earl
  • 71
  • 1
  • 4
  • why the executables produce output without `stdout=PIPE`? – jfs Oct 06 '12 at 13:37
  • Maybe there are some ways to exploit pipes? Anyway they should have logged a big warning message saying "Output disabled for program.exe"... – Ed Earl Oct 06 '12 at 13:45
  • It is interesting. Does `hello.exe > output.txt` produce empty file, but mere `hello.exe` prints HELLO? The former in Python: `with open('output.txt', 'wb') as f: check_call(r'.\hello.exe',stdout=f)` the later might be emulated with pexpect module (its Windows analog) – jfs Oct 06 '12 at 14:02
  • I met the same problem, but I closed the firewall on my PC. I tried to launch the application through java, C or python, but none of them could catch the launched app's output. Do you have any ideas? I've been stuck. – Haifeng Li Jan 15 '13 at 09:39