![](/img/trans.png)
[英]subprocess.CalledProcessError: ... returned non-zero exit status 255
[英]subprocess.CalledProcessError: returned non-zero exit status 1, while os.system does not raise any error
给定以下命令:
newman run tests.postman_collection.json -e environment.json --reporters testrail,json,html
提高:
RuntimeError: command 'newman run tests.postman_collection.json -e environment.json --reporters testrail,json,html
' return with error (code 1): b'\nhttps://host.testrail.io/index.php?/runs/view/1234\n'
执行命令的py代码:
try:
newmanCLI_output = subprocess.check_output(npmCLi, shell=True).decode().strip()
except subprocess.CalledProcessError as e:
raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))
是的,我确实使用了 check_output 返回值。
output 是一个 url 来测试铁路报告
那是os.system
的一个错误特征; 它返回退出代码以便您可以对其进行检查,但如果出现问题则不会引发错误。
subprocess.check_output
中的check
意味着检查命令是否成功,否则引发异常。 这通常是一件好事,因为您不希望进程在没有警告的情况下在您的下面死掉。
但是如果你想禁用它,你可以使用subprocess.run
解决它;
import shlex
result = subprocess.run(shlex.split(npmCLi), text=True, capture_output=True)
newmanCLI_output = result.stdout
避免shell=True
并使用shlex.split
来解析字符串的开关并不重要,但希望能够演示如何正确地执行这些操作。
您仍然应该了解命令失败的确切原因,以及忽略失败是否安全。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.