简体   繁体   English

subprocess.check_output()中stderr = subprocess.STDOUT的用途是什么?

[英]What is the use of stderr=subprocess.STDOUT in subprocess.check_output()?

I am writing a code in Python that is supposed to run some commands. 我正在用Python编写一个应该运行一些命令的代码。 To do this I'm using subprocess.check_output() as it allows to have a timeout. 为此,我使用subprocess.check_output(),因为它允许超时。 I understand how it works and what parameters we are supposed to give to it however I saw online that people are using "stderr=subprocess.STDOUT" a lot and I was wondering what it does and if I should use it or not. 我理解它是如何工作的以及我们应该给它的参数但是我在网上看到人们正在使用“stderr = subprocess.STDOUT”很多我想知道它做了什么以及我是否应该使用它。

I'm using Python 3.7 我正在使用Python 3.7

Here is an example of code: 这是一个代码示例:

import subprocess as sp

out = sp.check_output("ls not_existing_folder", 
                      stderr=sp.STDOUT, 
                      universal_newlines=True, 
                      timeout=15)

So I tried running it with and without the "stderr=sp.STDOUT" line but it doesn't appear to change anything. 所以我尝试使用和不使用“stderr = sp.STDOUT”行运行它,但它似乎没有改变任何东西。 I saw online that it's supposed to catch the error message, preventing a display in the console. 我在网上看到它应该捕获错误消息,阻止在控制台中显示。 I'm not sure I got that right... Can someone explain me the use of this piece of code? 我不确定我是否正确......有人可以解释我使用这段代码吗?

它不是像往常一样将其报告为错误,而是将其作为标准输出打印到控制台。

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

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