如何绕过python中sys.exit()的0-255范围限制? [英] How to bypass the 0-255 range limit for sys.exit() in python?
问题描述
在python(在Linux系统上)中,我正在使用os.system()
启动命令并获取返回码.如果该返回码不同于0,我想使用相同的返回码退出程序.所以我写道:
In python (on a Linux system), I'm launching a command using os.system()
and retrieving the return code. If that return code is different from 0, I would like to make the program exit with that same return code. So I wrote:
ret = os.system(cmd)
if ret != 0:
print "exit with status %s" % ret
sys.exit(ret)
当返回码小于256时,它可以正常工作,但是当返回码大于255时,使用的退出码为0.如何使sys.exit()接受大于255的代码?
When the return code is lower than 256, it works fine, but when it's greater than 255, the exit code used is 0. How can I make sys.exit() accept codes greater than 255?
限制实际上是255
实际上,ret
变量接收256,但是sys.exit()
无法使用它,因此程序将返回0.手动启动cmd
时,我看到它返回1,而不是256.
In fact, the ret
variable receives 256, but sys.exit()
fails to use it, so the program returns 0 instead. When I launch cmd
manually, I see that it returns 1, not 256.
推荐答案
为我工作(CentOS 5.5,Python 2.6.5):
Works for me (CentOS 5.5, Python 2.6.5):
$ python
>>> import sys
>>> sys.exit(245)
$ echo $?
245
这篇关于如何绕过python中sys.exit()的0-255范围限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!