Python 如何抑制或捕获 subprocess.run() 的输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41171791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to suppress or capture the output of subprocess.run()?
提问by planetp
From the examples in docs on subprocess.run()
it seems like there shouldn't be any output from
从文档中的示例subprocess.run()
来看,似乎不应该有任何输出
subprocess.run(["ls", "-l"]) # doesn't capture output
However, when I try it in a python shell the listing gets printed. I wonder if this is the default behaviour and how to suppress the output of run()
.
但是,当我在 python shell 中尝试它时,列表会被打印出来。我想知道这是否是默认行为以及如何抑制run()
.
回答by SethMMorton
Here is how to suppressoutput, in order of decreasing levels of cleanliness. They assume you are on Python 3.
以下是按清洁度级别递减的顺序抑制输出的方法。他们假设您使用的是 Python 3。
- You can redirect to the special
subprocess.DEVNULL
target.
- 您可以重定向到特殊
subprocess.DEVNULL
目标。
import subprocess
subprocess.run(['ls', '-l'], stdout=subprocess.DEVNULL)
# The above only redirects stdout...
# this will also redirect stderr to /dev/null as well
subprocess.run(['ls', '-l'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# Alternatively, you can merge stderr and stdout streams and redirect
# the one stream to /dev/null
subprocess.run(['ls', '-l'], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
- If you want a fully manual method, can redirect to
/dev/null
by opening the file handle yourself. Everything else would be identical to method #1.
- 如果你想要一个完全手动的方法,可以
/dev/null
通过自己打开文件句柄来重定向到。其他一切都与方法#1 相同。
import os
import subprocess
with open(os.devnull, 'w') as devnull:
subprocess.run(['ls', '-l'], stdout=devnull)
Here is how to captureoutput (to use later or parse), in order of decreasing levels of cleanliness. They assume you are on Python 3.
以下是如何捕获输出(稍后使用或解析),按清洁度级别的递减顺序排列。他们假设您使用的是 Python 3。
- If you simply want to capture both STDOUT and STDERR independently, AND you are on Python >= 3.7, use
capture_output=True
.
- 如果您只想独立捕获 STDOUT 和 STDERR,并且您使用的是 Python >= 3.7,请使用
capture_output=True
.
import subprocess
result = subprocess.run(['ls', '-l'], capture_output=True)
print(result.stdout)
print(result.stderr)
- You can use
subprocess.PIPE
to capture STDOUT and STDERR independently.
- 您可以使用
subprocess.PIPE
独立捕获 STDOUT 和 STDERR。
import subprocess
result = subprocess.run(['ls', '-l'], stdout=subprocess.PIPE)
print(result.stdout)
# To also capture stderr...
result = subprocess.run(['ls', '-l'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(result.stdout)
print(result.stderr)
# To mix stdout and stderr into a single string
result = subprocess.run(['ls', '-l'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print(result.stdout)
NOTE: By default, captured output is returned as bytes
. If you want to capture as text (e.g. str
), use universal_newlines=True
(or on Python >=3.7, use the infinitely more clear and easy-to-understand option text=True
- it's the same a universal_newlines
but with a different name).
注意:默认情况下,捕获的输出返回为bytes
. 如果您想捕获为文本(例如str
),请使用universal_newlines=True
(或在 Python >=3.7 上,使用无限清晰且易于理解的选项text=True
-universal_newlines
与 a相同,但名称不同)。