与返回 stdout 内容的 subprocess.check_call 等效的好方法是什么?
我想要一个与 subprocess.check_call 接口相匹配的好方法 - 即,它在失败时抛出 CalledProcessError
,是同步的,&c - 但相反返回命令的返回代码(如果它确实这样做)返回程序的输出,要么仅是 stdout,要么是 (stdout, stderr) 的元组。
有人有这样做的方法吗?
I'd like a good method that matches the interface of subprocess.check_call
-- ie, it throws CalledProcessError
when it fails, is synchronous, &c -- but instead of returning the return code of the command (if it even does that) returns the program's output, either only stdout, or a tuple of (stdout, stderr).
Does somebody have a method that does this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Python 2.7+
Python < 2.7
来自 subprocess.py:
另请参阅 将系统命令输出捕获为字符串,作为可能的另一个示例
check_output()
实现。Python 2.7+
Python < 2.7
From subprocess.py:
See also Capturing system command output as a string for another example of possible
check_output()
implementation.我无法在评论中获取格式,所以这是对 JF Sebastian 的回答
我发现这非常有帮助,所以我想我会添加到这个答案中。我希望能够在代码中无缝工作而无需检查版本。这就是我所做的...
我将上面的代码放入一个名为“subprocess_compat.py”的文件中。然后在我使用子进程的代码中我做了。
现在,在代码中的任何位置,我都可以使用我想要的参数调用“subprocess.check_output”,并且无论我使用哪个版本的 python,它都可以工作。
I can not get formatting in a comment, so this is in response to J.F. Sebastian's answer
I found this very helpful so I figured I would add to this answer. I wanted to be able to work seamlessly in the code without checking the version. This is what I did...
I put the code above into a file called 'subprocess_compat.py'. Then in the code where I use subprocess I did.
Now anywhere in the code I can just call 'subprocess.check_output' with the params I want and it will work regardless of which version of python I am using.
在我读了两遍之后,我意识到它已经有十年的历史了,并且大多数答案适用于现已弃用的 python2.7 而不是 python3。
现在我们正在 - 或者应该 - 在 python3 上,似乎 python >= 3.7 的最佳选择是使用多个评论中提到的以下内容:
为了节省您搜索更多详细信息,我推荐我找到的答案SethMMorton 在 中提供了精彩的细节回答“如何抑制或捕获 subprocess.run() 的输出?” 正如那里所述,您可以直接访问 stdout、stderr:
如果您需要支持 Python 3.6:
此信息来自
威廉·范·昂塞姆的相关问题的回答。
我倾向于直接访问 https://docs.python.org/3/library/ subprocess.html 来刷新我对一般子流程事物的记忆。 (不过,这些示例通常对我来说更容易快速访问。)
After I read this twice, I realized it's ten years old and most answers apply to the now deprecated python2.7 rather than python3.
Now that we are - or should be - on python3, it seems that the best option for python >= 3.7 is to use the following as is mentioned in multiple comments:
To save you searching for more details, I recommend an answer I found with wonderful detail by SethMMorton in an answer to "How to suppress or capture the output of subprocess.run()?" As described there, you can access stdout, stderr directly as:
If you need to support Python 3.6:
This info is from
Willem Van Onsem's answer to a related question.
I tend to go straight to https://docs.python.org/3/library/subprocess.html to refresh my memory on general subprocess things. (The SO examples are often easier for me to access quickly though.)
该函数以字符串列表的形式返回终端输出。
This function returns terminal output in the form of list of string.