Linux subprocess.call() 的返回值是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1696998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the return value of subprocess.call()?
提问by anonymous
I am not sure what the return value of subprocess.call()
means.
我不确定返回值是什么subprocess.call()
意思。
Can I safely assume a zero value will always mean that the command executed successfully?
Is the return value equivalent to the exit staus of a shell command?
我可以安全地假设零值总是意味着命令成功执行吗?
返回值是否等同于 shell 命令的退出状态?
For example, will the following piece of code work for virtually any command on Linux?
例如,以下代码对 Linux 上的几乎所有命令都有效吗?
cmd = "foo.txt > bar.txt"
ret = subprocess.call(cmd, shell=True)
if ret != 0:
if ret < 0:
print "Killed by signal", -ret
else:
print "Command failed with return code", ret
else:
print "SUCCESS!!"
Please enlighten me :-)
请赐教:-)
回答by Jochen Ritzel
Yes, Subprocess.call
returns "actual process return code".
是的,Subprocess.call
返回“实际进程返回代码”。
You can check official documentation of Subprocess.calland Subprocess.Popen.returncode
回答by Ned Batchelder
It is the return code, but keep in mind it's up to the author of the subprocess what the return code means. There is a strong culture of 0 meaning success, but there's nothing enforcing it.
它是返回码,但请记住,返回码的含义取决于子流程的作者。0 意味着成功是一种强烈的文化,但没有任何东西可以强制执行。
回答by unutbu
You are at the mercy of the commands that you call. Consider this:
您受制于您所调用的命令。考虑一下:
test.py
测试文件
#!/usr/bin/env python
success=False
if not success:
exit()
Then running your code (with cmd='test.py') will result in SUCCESS!!
然后运行您的代码(使用 cmd='test.py')将导致成功!!
merely because test.py does not conform to the convention of returning a non-zero value when it is not successful.
仅仅是因为 test.py 不符合在不成功时返回非零值的约定。