Redirect subprocess stderr to stdout

亡梦爱人 提交于 2019-11-27 03:02:16

问题


I want to redirect the stderr output of a subprocess to stdout. The constant STDOUT should do that, shouldn't it?

However,

$ python >/dev/null -c 'import subprocess;\
                        subprocess.call(["ls", "/404"],stderr=subprocess.STDOUT)'

does output something. Why is that the case, and how do I get the error message on stdout?


回答1:


A close read of the source code gives the answer. In particular, the documentation is misleading when it says:

subprocess.STDOUT
Special value that (...) indicates that standard error should go into the same handle as standard output.

Since stdout is set to "default" (-1, technically) when stderr=subprocess.STDOUT is evaluated, stderr is set to "default" as well. Unfortunately, this means that stderr output still goes to stderr.

To solve the problem, pass in the stdout file instead of subprocess.STDOUT:

$ python >/dev/null -c 'import subprocess,sys;subprocess.call(["ls", "/404"],
                        stderr=sys.stdout.buffer)'

Or, for compatibility with legacy 2.x versions of Python:

$ python >/dev/null -c 'import subprocess,sys;subprocess.call(["ls", "/404"],
                        stderr=sys.stdout.fileno())'



回答2:


Actually, using subprocess.STDOUT does exactly what is stated in the documentation: it redirects stderr to stdout so that e.g.

proc = subprocess.Popen(self.task["command"], shell=False, bufsize=1, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = ""
while (True):
    # Read line from stdout, break if EOF reached, append line to output
    line = proc.stdout.readline()
    line = line.decode()
    if (line == ""): break
    output += line

results in variable output containing the process' output from both stdout and stderr.

stderr=subprocess.STDOUT redirects all stderr output directly to stdout of the calling process, which is a major difference.



来源:https://stackoverflow.com/questions/11495783/redirect-subprocess-stderr-to-stdout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!