首页 > 解决方案 > subprocess.run() 不返回 stdout 或 stderr

问题描述

我尝试subprocess.run 按照此答案中的描述使用,但它没有为 stdout 或 stderr 返回任何内容:

>>> result = subprocess.run('echo foo', shell=True, check=True)
>>> print(result.stdout);
None
>>> print(result.stderr);
None

我也尝试过使用capture_output=True,但我得到了一个例外__init__() got an unexpected keyword argument 'capture_output',即使它在文档中有所描述。

标签: pythonpython-3.xpython-3.6

解决方案


我犯了一个错误,我没有添加stdout=subprocess.PIPE

result = subprocess.run('echo foo', shell=True, check=True, stdout=subprocess.PIPE);

现在它正在工作。


推荐阅读