我在用:
grepOut = subprocess.check_output("grep " + search + " tmp", shell=True)
要运行终端命令,我知道我可以使用 try/ except 来捕获错误,但如何获取错误代码的值?
我在官方文档中找到了这个:
exception subprocess.CalledProcessError
Exception raised when a process run by check_call() or check_output() returns a non-zero exit status.
returncode
Exit status of the child process.
但没有给出任何例子,谷歌也没有帮助。
您可以从引发的异常中获取错误代码和结果。
这可以通过字段来完成returncode
and output
.
例如:
import subprocess
try:
grepOut = subprocess.check_output("grep " + "test" + " tmp", shell=True)
except subprocess.CalledProcessError as grepexc:
print("error code", grepexc.returncode, grepexc.output)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)