Skip to main content
 首页 » 编程设计

python之返回值超出 sys.exit 默认间隔 [0-127]

2025年05月04日20kerrycode

有没有办法从一个简单的 python 脚本中以任意值退出?实际上,我想返回一个端口号。我也在这里和文档中阅读过,但只找到了解决方法......

请您参考如下方法:

unix exit 代码是一个byte。大于 127 的值被保留。不,您无法通过此 hack 可靠地传递端口号。为什么不直接将它打印到标准输出 并解析该输出?

https://www.gnu.org/software/libc/manual/html_node/Exit-Status.html

A general convention reserves status values 128 and up for special purposes. In particular, the value 128 is used to indicate failure to execute another program in a subprocess. This convention is not universally obeyed, but it is a good idea to follow it in your programs.

[...] the status value is truncated to eight bits