[英]proper way to check exit codes python3
I have a var exitcd
that executes a command.我有一个执行命令的 var
exitcd
。 Its supposed to get the exit code
of running the command and then fall into one of the if/elif conditions based on that exit code.它应该获取运行命令的
exit code
,然后根据该退出代码陷入 if/elif 条件之一。
However, it is failing stating general error occurred
when the -d
being used is telling me it should be hitting elif exitcd == 3
but that is not occurring.但是,当正在使用的
-d
告诉我它应该击中elif exitcd == 3
但没有general error occurred
。
exitcd=os.popen('cmd test ').read()
print(exitcd)
#print("retcode was {}".format(retcode))
#not found
if exitcd == 0:
continue
# found
elif exitcd == 1:
subprocess.Popen('cmd test -d --insecure --all-projects --json >> a_dir_res_storage')
subprocess.Popen('cmd monitor --all-projects')
continue
#error with command
elif exitcd == 2:
print('error with command')
continue
#failure to find project manifest
elif exitcd == 3:
print('error with find manifests for {}'.format(storage+'/'+a_dir))
continue
else:
print('general error occurred.')
what am i doing wrong here?我在这里做错了什么?
Thanks谢谢
Here's a super crude example of how I've typically run external commands from Python.这是一个超级粗略的示例,说明我通常如何从 Python 运行外部命令。 Using the
communicate
method not only allows you to get the return code from the command, but the output sent to stdout
and stderr
are independent.使用
communicate
方法不仅可以让你从命令中获取返回码,而且发送到stdout
和stderr
的output是独立的。
from subprocess import Popen, PIPE
#
cmd_fails = ["/bin/false"]
run_fails = Popen(cmd_fails, stdout=PIPE, stderr=PIPE)
out, err = run_fails.communicate()
rc = run_fails.returncode
print(f"out:{out}\nerr:{err}\nrc={rc}")
#
cmd_works = ["/bin/echo", "Hello World!"]
run_works = Popen(cmd_works, stdout=PIPE, stderr=PIPE)
out, err = run_works.communicate()
rc = run_works.returncode
print(f"out:{out}\nerr:{err}\nrc={rc}")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.