gpt4 book ai didi

python - Python 中 os.system() 的返回值是什么?

转载 作者:IT老高 更新时间:2023-10-28 21:37:03 25 4
gpt4 key购买 nike

我遇到了这个:

>>> import os
>>> os.system('ls')
file.txt README
0

os.system() 的返回值是多少? ?为什么我得到 0?

最佳答案

os.system 的返回值依赖于操作系统。

在 Unix 上,返回值是一个 16 位数字,其中包含两条不同的信息。来自文档:

a 16-bit number, whose low byte is the signal number that killed the process, and whose high byte is the exit status (if the signal number is zero)

因此,如果信号编号(低字节)为 0,理论上,将结果移位 8 位(result >> 8)以获得错误代码是安全的。函数os.WEXITSTATUS正是这样做的。如果错误码为0,则通常表示进程无错误退出。

在 Windows 上,文档指定 os.system 的返回值取决于 shell。如果shell是cmd.exe(默认),则值为进程的返回码。同样,0 表示没有错误。

对于其他错误代码:

关于python - Python 中 os.system() 的返回值是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6466711/

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