简体   繁体   English

具有“ 1>&2”和stderr = STDOUT的Python子进程

[英]Python's subprocess with “1>&2” and stderr=STDOUT

I have this code from https://pymotw.com/2/subprocess/ 我有来自https://pymotw.com/2/subprocess/的代码

I'm not sure how to interpret the code, in the check_output with 1>&2 output is redirected to stderr, but in the parameter, the stderr is back to stdout stderr=subprocess.STDOUT . 我不确定如何解释代码,在带有1>&2输出的check_output重定向到stderr,但是在参数中,stderr返回到stdout stderr=subprocess.STDOUT

output = subprocess.check_output(
    'echo to stdout; echo to stderr 1>&2; exit 1',
    shell=True,  
    stderr=subprocess.STDOUT, 
    )
print "*****************"
print 'Have %d bytes in output' % len(output)
print output

Running the code, the print commands are not executed meaning nothing is captured. 运行代码,不会执行打印命令,这意味着什么也不会捕获。

What does this code trying to accomplish? 该代码试图完成什么?

EDIT 编辑

From the answer and comment, I could run this code to get 从答案和评论中,我可以运行以下代码来获取

try:
    output = subprocess.check_output(
        'echo to stdout; echo to stderr 1>&2; exit 1',
        shell=True,  # No such file or directory error without, maybe 1>&2 requires shell=True
        stderr=subprocess.STDOUT,  
        )

except subprocess.CalledProcessError as e:

    print "*****************"
    print 'Have %d bytes in output' % len(e.output)
    print e.output

this output: 此输出:

*****************
Have 20 bytes in output
to stdout
to stderr

However, when I commented out the stderr=subprocess.STDOUT line, I got instead 但是,当我注释掉stderr=subprocess.STDOUT行时,我得到了

to stderr
*****************
Have 10 bytes in output
to stdout

EDIT2 EDIT2

I tested more with stderr library ( https://github.com/sickill/stderred ) that helps a shell to show characters from stderr in red color. 我使用stderr库( https://github.com/sickill/stderred )进行了更多测试,该库有助于外壳将stderr中的字符显示为红色。

When I execute this code (comment out the redirection), I can see the to stderr in BLACK color which implies it uses stdout. 当我执行此代码(注释掉重定向)时,我可以看到to stderr为黑色,这意味着它使用了stdout。

output = subprocess.check_output(
        'echo to stdout; echo to stderr 1>&2; exit 1',
        shell=True,  
        #stderr=subprocess.STDOUT, 
        )

From this, I guess (correct me if I'm wrong) that Python's check_output method prints out the data into the stderr redirect to stdout so that it prints out the error message into stderr. 由此,我想(如果我错了,请纠正我)Python的check_output方法将数据输出到stderr重定向到stdout中,以便将错误消息输出到stderr中。

在此处输入图片说明

The 1 >&2 shell code applies only to the (echo) command it appears on. 1 >&2 Shell代码仅适用于其上显示的(echo)命令。 It is how to tell the shell to direct the output of that echo to the shell's stderr stream. 这是告诉外壳将回显的输出定向到外壳的stderr流的方法。

The python code stderr=subprocess.STDOUT tells the subprocess module that you want the process's stderr stream to be the same file descriptor as its stdout stream so that you will read whatever the process writes to either stream interleaved together in one stream. python代码stderr=subprocess.STDOUT告诉子进程模块,您希望该进程的stderr流与其stdout流是相同的文件描述符,以便您可以读取该进程写入到在一个流中交织在一起的任何流的任何内容。

The exit 1 in the shell command means that the shell exits with an error (non-zero) status. shell命令中的exit 1表示外壳程序以错误(非零)状态退出。

The purpose of the code is to demonstrate that the python function subprocess.check_output will check the exit status and raise an exception when it is non-zero. 该代码的目的是演示python函数subprocess.check_output将检查退出状态并在非零时引发异常。

If the exit code was non-zero it raises a CalledProcessError. 如果退出代码不为零,则会引发CalledProcessError。 The CalledProcessError object will have the return code in the returncode attribute and output in the output attribute. CalledProcessError对象将在returncode属性中具有返回码,并在output属性中具有输出。

Your description of: 您对以下内容的描述:

Running the code, the print commands are not executed 运行代码,不执行打印命令

is a bit misleading since you neglect to mention the output that does occur: 这有点误导,因为您忽略了确实发生的输出:

Traceback (most recent call last):
  File "t.py", line 6, in <module>
    stderr=subprocess.STDOUT, 
  File "/usr/lib/python2.7/subprocess.py", line 573, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command 'echo to stdout; echo to stderr 1>&2; exit 1' returned non-zero exit status 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM