Python中os.system()的返回值是什么? [英] What is the return value of os.system() in Python?
问题描述
我碰到了这个:
>>> import os
>>> os.system('ls')
file.txt README
0
os.system()
的返回值是什么?为什么我得到0?
What is return value of os.system()
? Why I get 0?
推荐答案
os.system
与操作系统有关.
The return value of os.system
is OS-dependant.
在Unix上,返回值是一个16位数字,包含两个不同的信息.从文档中:
On Unix, the return value is a 16-bit number that contains two different pieces of information. From the documentation:
一个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,则通常表示该进程已退出且没有错误.
So if the signal number (low byte) is 0, it would, in theory, be safe to shift the result by 8 bits (result >> 8
) to get the error code. The function os.WEXITSTATUS
does exactly this. If the error code is 0, that usually means that the process exited without errors.
在Windows上,文档指定os.system
的返回值取决于外壳.如果外壳程序是cmd.exe
(默认值),则该值是进程的返回码.同样,0表示没有错误.
On Windows, the documentation specifies that the return value of os.system
is shell-dependant. If the shell is cmd.exe
(the default one), the value is the return code of the process. Again, 0 would mean that there weren't errors.
其他错误代码:
- 在Linux上 在Windows上
- on Linux
- on Windows
这篇关于Python中os.system()的返回值是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!