subprocess unable to capture STDOUT - what could be program be doing?
I have this program, which when executed on the console like this:
prog 1> output 2> error
Has the valid output and error. However, when I execute the same program using the subprocess module.
p = subprocess.Popen(['prog'],stdout=PIPE, stderr=PIPE,close_fds=True)
out, err = p.communicate()
The开发者_StackOverflow out is empty but err is proper. What could be happening here? I can do a os.system and direct to output and error too. But I had been relying on subprocess for doing till recently.
What could be the problem? This is being tried on Linux only. Not on Windows. `
Are you trying this on Windows?
The use of close_fds
is
platform dependent, according to the subprocess.Popen()
doc.
If
close_fds
is true, all file descriptors except 0, 1 and 2 will be closed before the child process is executed. (Unix only). Or, on Windows, if close_fds is true then no handles will be inherited by the child process. Note that on Windows, you cannot set close_fds to true and also redirect the standard handles by setting stdin, stdout or stderr.
Just tried your code and works for me:
>>> p = subprocess.Popen(['ls'], stdout=subprocess.PIPE, stderr=subprocess.PIPE,
close_fds=True)
>>> out, err = p.communicate()
>>> out
'build\nCode\n...'
>>> err
''
a) Make sure your program is called correctly.
b) Did you import PIPE correctly?
精彩评论