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

标签 python operating-system

我遇到了这个:

>>> 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/

相关文章:

python - 使用 Python URLParse Parse_qs 解析 URL

python - Holoviews:如何用时间索引绘制 DataFrame

memory-management - Linux内核如何找到脏页来刷新?

operating-system - 为什么在临界区中休眠是并发问题?

python导入函数不导入程序

python - Cassandra 未完成与 EventletConnection 的连接

python - 我自己的 tensorflow MNIST 管道数据给出了 ValueError : input elements number isn't divisible by 65536

memory-management - 如何理解IBM/370的内存保护方案?

c++ - 使用 fork() 的子进程和父进程

operating-system - 处理器如何知道一条指令正在进行系统调用