[英]In Python 2 or 3, how to get both the return code and return string of system call?
I know I can do this to get the execute a system command, eg, make, and it'll give me either a 0 for success, or a non-zero for a failure. 我知道我可以这样做来执行一个系统命令,例如make,它会给我一个0表示成功,或者一个非零表示失败。
import os
result = os.system('make')
I also know I can do this so I can see the return string of the command 我也知道我可以这样做,所以我可以看到命令的返回字符串
import commands
result = commands.getoutput('make')
How can I accomplish both, where I get both the return code and the return string result, so then I can 如何同时获得返回代码和返回字符串结果,我怎么能完成两者,所以我可以
if return_code > 0:
print(return_string)
Thanks. 谢谢。
The canonical way to run stuff with Python is to use the subprocess
module, but it has a good bunch of functions enigmatically called check_call
or check_output
and these functions tend to have cryptic warnings like "Do not use stdout=PIPE or stderr=PIPE with this function", so let me provide some more: 使用Python运行东西的规范方法是使用
subprocess
进程模块,但它有一大堆check_call
的函数叫做check_call
或check_output
,这些函数往往会有一些神秘的警告,例如“不要使用stdout = PIPE或stderr = PIPE功能“,所以让我提供更多:
proc = subprocess.Popen(["your_command", "parameter1", "paramter2"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Now the process is running in the background and you have a reference to it. 现在该过程在后台运行,您可以参考它。
EDIT: I almost forgot - if you'd like to retrieve the output later, you have to tell Python to create reading pipes for standard output. 编辑:我差点忘了 - 如果你想稍后检索输出,你必须告诉Python为标准输出创建读取管道。 If you don't do this step, stdout and stderr will just go to your program's standard output and standard error, and
communicate
won't pick them up in step 2. 如果不执行此步骤,stdout和stderr将只执行程序的标准输出和标准错误,并且
communicate
将不会在步骤2中接收它们。
stdout, sterr = proc.communicate()
return_code = proc.returncode
communicate
allows you to do some more things too: communicate
可以让你做更多的事情:
input=
parameter) input=
parameter) timeout=
parameter) timeout=
参数) Make sure to also catch and properly handle any exceptions from Popen
or communicate
. 确保捕获并正确处理来自
Popen
任何异常或进行communicate
。
If you don't care about old Python, there's a simpler method called subprocess.run
that Does It All: 如果您不关心旧的Python,那么有一个更简单的方法叫做
subprocess.run
,它可以全部:
completed_process = subprocess.run(
['your_command', 'parameter'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
# this starts the process, waits for it to finish, and gives you...
completed_process.returncode
completed_process.stdout
completed_process.stderr
For error-checking you can call completed_process.check_returncode()
or just pass check=True
as an additional argument to run
. 对于错误检查,您可以调用
completed_process.check_returncode()
或只传递check=True
作为run
的附加参数。
Another possibly simpler method is: 另一个可能更简单的方法是:
import subprocess
try:
output = subprocess.check_output("make", stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print('return code =', e.returncode)
print(e.output)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.