python - 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'
,即使它在文档中有所描述。
解决方案
我犯了一个错误,我没有添加stdout=subprocess.PIPE
:
result = subprocess.run('echo foo', shell=True, check=True, stdout=subprocess.PIPE);
现在它正在工作。
推荐阅读
- linux - 在 Azure VM 上使用 cloud-init 挂载数据磁盘失败
- c# - 为 RichText 框实现语法高亮(类似于 VS 中的 C#)
- swift - 将 Swift 数组转换为 Couchbase lite ArrayObject
- javascript - 为什么 document.activeElement.matches(':focus') 返回 false?
- python - 如何改变 np.random.normal 的颜色?
- ios - 在 iOS 中限制场景套件相机围绕单个(y)轴旋转
- r - 在两列的累积信息之间创建一个汇总表
- algorithm - 如何在无根树中执行路径查询?
- python - 在 Python 中向 Trie 类添加单词
- c# - 通过MVC应用程序将电子表格数据导入SQL数据库,有没有办法在导入期间为不在电子表格上的列分配值?