gpt4 book ai didi

python - CalledProcessError 退出状态代码 5

转载 作者:行者123 更新时间:2023-12-01 09:16:36 28 4
gpt4 key购买 nike

我一直在使用一个执行 bash 命令的简短 python 脚本。该程序运行了大约一个月,效果良好。最近,我尝试运行脚本并向其传递以下命令:

my_launcher.py -c /path/to/config/file.json

(顺便说一句,当在终端中输入命令时,我没有导致错误并且运行正常)并且收到以下消息:

RuntimeError: Command '['bash', '-c', 'my_launcher.py -c /path/to/config/file.json']' returns non-zero exit status (code5)

在 Google 上查找后,我找到了返回代码 0、1 和 2 的定义,但没有找到代码 5 的定义。想知道你们是否知道任何相关信息。这是什么意思?怎么解决呢?等等

这是导致错误的Python代码:

try :
#check_output produces byte string
#raises exception if command returns a non-zero exit status (error occurs during processing of command)
string_of_text_rc = subprocess.check_output(['bash', '-c', bashCommand])
except subprocess.CalledProcessError as e:
raise RuntimeError("Command '{}' returns non-zero exit status (code{})".format(e.cmd, e.returncode))

删除 try/except 时,这是回溯:

Traceback (most recent call last):
File "bash_cmd.py", line 27, in <module>
run_cmd('my_launcher.py -c /path/to/config/file.json')
File "bash_cmd.py", line 17, in run_cmd
string_of_text_rc = subprocess.check_output(['bash', '-c', bashCommand])
File "/usr/lib64/python2.7/subprocess.py", line 575, in check_output
raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['bash', '-c', 'my_launcher.py -c /path/to/config/file.json']' returned non-zero exit status 5

**编辑:正确的输出包含在 e.output 中。这意味着该命令已运行并返回正确的输出。我真的不知道为什么会收到此错误代码。

最佳答案

郑重声明,以下是运行 .py 文件的方式:

result = subprocess.check_output([
sys.executable, 'my_launcher.py', '-c', path_to_json])

以下是运行 shell 命令的方法:

result = subprocess.check_output(bash_command, shell=True)

对于您的问题 - 您能否从代码中删除 try/except ,以便我们可以看到完整的错误回溯?那里可能隐藏着一些好的信息。

关于python - CalledProcessError 退出状态代码 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51192313/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com