What is the return value of os.system() in Python?(Python 中 os.system() 的返回值是什么?)
问题描述
我遇到了这个:
>>> 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 的返回值取决于 shell.如果shell是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 上李>
这篇关于Python 中 os.system() 的返回值是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python 中 os.system() 的返回值是什么?
基础教程推荐
- 尝试制作WhatsApp机器人 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
