python os.system保存返回值_python中os.system的返回值

python中os.system()的返回值

1、python中的 os.system(cmd)的返回值与linux命令返回值(具体参见本文附加内容)的关系

你们都习惯用os.systemv()函数执行linux命令,该函数的返回值十进制数(分别对应一个16位的二进制数)。该函数的返回值与 linux命令返回值二者的转换关系为:该函数的返回值(十进制)转化成16二进制数,截取其高八位(若是低位数是0的状况下,有关操做系统的错误码共 131个,因此低位都是零),而后转乘十进制数即为 linux命令返回值0。node

例如:python

os.system()返回值为0 linux命令返回值也为0.linux

os.system()返回值为256,十六位二进制数示为:00000001,00000000,高八位转乘十进制为 1 对应linux命令返回值 1web

os.system()返回值为512,十六位二进制数示为:00000010,00000000,高八位转乘十进制为 2 对应 linux命令返回值 2app

…其它同理…dom

os.system()返回值为32512,十六位二进制数示为:01111111,00000000,高八位转乘十进制为 127 对应 linux命令返回值 127socket

问题:/bin/xxx.py是一个返回码为1的程序。当python 程序使用os.system(”./bin/xxx.py”) 这样调用的时候, 成功运行后os.system 的返回值出现了问题,变成了256 ,也就是0×100。而不是正常应该返回的1。ide

解决:查阅了文档发现os.system()的返回为:svg

On Unix, the return value is the exit status of the process encoded in the format specified for wait().

而os.wait()的返回为:函数

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);

os.system的返回值并非执行程序的返回结果。而是一个16位的数,它的高位才是返回码。也就是说os.system()返回256即0×0100,返回码应该是其高位0×01即1。因此要获取程序运行退出的值(好比C的main函数中的return 0),须要处理一下。

ret = os.system('./a.out')

ret >>= 8

这样才能获取到正确的返回值。另外还要注意:python获取到的值是无符号整数,因此返回负值的时候,打印出来是很大的正值。好比返回-1,python 会获取到255, -2则254,以此类推。

因此最好就判断是否为0就能够了,实在要判断本身写的c程序返回值,建议返回0,1,2,3等值,出错返回-1。

另外,我遇到一次明明处理好了返回值,c程序调试信息提示也该返回值0了,结果python获取到的是 -1,并且不管c程序返回多少,python都获取-1。后来排查c程序的问题,发现原来是由于我这个python程序自己是由另外一个C程序调用的,而调 用它的那个C程序中将SIGCLD信号忽略了(这代表python是根据子进程退出时产生的信号来获取返回值的),我将那个C程序的SIGCLD绑定到函 数,即便那个函数什么也不作,python也能获取到正确的返回值了。

2、 linux命令执行后不管成功与否都有一个返回值:

若是为 0,则表示命令执行成功,其它值则表示错误,具体的错误码含义以下:

"OS error code 1: Operation not permitted"

"OS error code 2: No such file or directory"

"OS error code 3: No such process"

"OS error code 4: Interrupted system call"

"OS error code 5: Input/output error"

"OS error code 6: No such device or address"

"OS error code 7: Argument list too long"

"OS error code 8: Exec format error"

"OS error code 9: Bad file descriptor"

"OS error code 10: No child processes"

"OS error code 11: Resource temporarily unavailable"

"OS error code 12: Cannot allocate memory"

"OS error code 13: Permission denied"

"OS error code 14: Bad address"

"OS error code 15: Block device required"

"OS error code 16: Device or resource busy"

"OS error code 17: File exists"

"OS error code 18: Invalid cross-device link"

"OS error code 19: No such device"

"OS error code 20: Not a directory"

"OS error code 21: Is a directory"

"OS error code 22: Invalid argument"

"OS error code 23: Too many open files in system"

"OS error code 24: Too many open files"

"OS error code 25: Inappropriate ioctl for device"

"OS error code 26: Text file busy"

"OS error code 27: File too large"

"OS error code 28: No space left on device"

"OS error code 29: Illegal seek"

"OS error code 30: Read-only file system"

"OS error code 31: Too many links"

"OS error code 32: Broken pipe"

"OS error code 33: Numerical argument out of domain"

"OS error code 34: Numerical result out of range"

"OS error code 35: Resource deadlock avoided"

"OS error code 36: File name too long"

"OS error code 37: No locks available"

"OS error code 38: Function not implemented"

"OS error code 39: Directory not empty"

"OS error code 40: Too many levels of symbolic links"

"OS error code 42: No message of desired type"

"OS error code 43: Identifier removed"

"OS error code 44: Channel number out of range"

"OS error code 45: Level 2 not synchronized"

"OS error code 46: Level 3 halted"

"OS error code 47: Level 3 reset"

"OS error code 48: Link number out of range"

"OS error code 49: Protocol driver not attached"

"OS error code 50: No CSI structure available"

"OS error code 51: Level 2 halted"

"OS error code 52: Invalid exchange"

"OS error code 53: Invalid request descriptor"

"OS error code 54: Exchange full"

"OS error code 55: No anode"

"OS error code 56: Invalid request code"

"OS error code 57: Invalid slot"

"OS error code 59: Bad font file format"

"OS error code 60: Device not a stream"

"OS error code 61: No data available"

"OS error code 62: Timer expired"

"OS error code 63: Out of streams resources"

"OS error code 64: Machine is not on the network"

"OS error code 65: Package not installed"

"OS error code 66: Object is remote"

"OS error code 67: Link has been severed"

"OS error code 68: Advertise error"

"OS error code 69: Srmount error"

"OS error code 70: Communication error on send"

"OS error code 71: Protocol error"

"OS error code 72: Multihop attempted"

"OS error code 73: RFS specific error"

"OS error code 74: Bad message"

"OS error code 75: Value too large for defined data type"

"OS error code 76: Name not unique on network"

"OS error code 77: File descriptor in bad state"

"OS error code 78: Remote address changed"

"OS error code 79: Can not access a needed shared library"

"OS error code 80: Accessing a corrupted shared library"

"OS error code 81: .lib section in a.out corrupted"

"OS error code 82: Attempting to link in too many shared libraries"

"OS error code 83: Cannot exec a shared library directly"

"OS error code 84: Invalid or incomplete multibyte or wide character"

"OS error code 85: Interrupted system call should be restarted"

"OS error code 86: Streams pipe error"

"OS error code 87: Too many users"

"OS error code 88: Socket operation on non-socket"

"OS error code 89: Destination address required"

"OS error code 90: Message too long"

"OS error code 91: Protocol wrong type for socket"

"OS error code 92: Protocol not available"

"OS error code 93: Protocol not supported"

"OS error code 94: Socket type not supported"

"OS error code 95: Operation not supported"

"OS error code 96: Protocol family not supported"

"OS error code 97: Address family not supported by protocol"

"OS error code 98: Address already in use"

"OS error code 99: Cannot assign requested address"

"OS error code 100: Network is down"

"OS error code 101: Network is unreachable"

"OS error code 102: Network dropped connection on reset"

"OS error code 103: Software caused connection abort"

"OS error code 104: Connection reset by peer"

"OS error code 105: No buffer space available"

"OS error code 106: Transport endpoint is already connected"

"OS error code 107: Transport endpoint is not connected"

"OS error code 108: Cannot send after transport endpoint shutdown"

"OS error code 109: Too many references: cannot splice"

"OS error code 110: Connection timed out"

"OS error code 111: Connection refused"

"OS error code 112: Host is down"

"OS error code 113: No route to host"

"OS error code 114: Operation already in progress"

"OS error code 115: Operation now in progress"

"OS error code 116: Stale NFS file handle"

"OS error code 117: Structure needs cleaning"

"OS error code 118: Not a XENIX named type file"

"OS error code 119: No XENIX semaphores available"

"OS error code 120: Is a named type file"

"OS error code 121: Remote I/O error"

"OS error code 122: Disk quota exceeded"

"OS error code 123: No medium found"

"OS error code 124: Wrong medium type"

"OS error code 125: Operation canceled"

"OS error code 126: Required key not available"

"OS error code 127: Key has expired"

"OS error code 128: Key has been revoked"

"OS error code 129: Key was rejected by service"

"OS error code 130: Owner died"

"OS error code 131: State not recoverable"

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/468473.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

你今天忘本了吗?

我从小在农村长大,我们村前面有一条很长的河,我家在河的后面的那个小山坡上,背靠着大山,很多大师来我们家,都说我家这里是风水宝地,幸运的是,我就在这块风水宝地里长大了。后来,我很…

评分组件开发

我们知道&#xff0c;许多外卖app都有评分的星星&#xff0c;这里我总结一下对评分组件的开发&#xff0c;学习视频&#xff1a;饿了么实战&#xff08;慕课网&#xff09; 1.html部分 <div class"star" :class"starType"><span v-for"itemC…

第4章 第三节 内核同步

抢占式内核和非抢占式内核Linux 内核有两个空间&#xff0c;一个是内核空间一个是用户空间&#xff0c;如果一个进程正在内核态执行的时候&#xff0c;允许内核打断他的执行&#xff0c;让另一个进程执行&#xff0c;那么这个内核就是可抢占式内核。还有一种情况就是&#xff0…

stream去重_Java中对List去重 Stream去重的解决方法

问题当下互联网技术成熟&#xff0c;越来越多的趋向去中心化、分布式、流计算&#xff0c;使得很多以前在数据库侧做的事情放到了java端。今天有人问道&#xff0c;如果数据库字段没有索引&#xff0c;那么应该如何根据该字段去重&#xff1f;大家都一致认为用java来做&#xf…

民企信息化建设个人经历(四)

两周没写了&#xff0c;上班第一天再来留下点记录。节前最后一周&#xff0c;又跟董事长报告了一次&#xff0c;感觉应该还算有点效果。上篇中提到的五个方向(仓库条码化(WMS)&#xff0c;生产条码化(MES)。財务相关模块启用。HR相关系统整合。PLM系统优化)&#xff0c;略微做了…

第四章 第四节 per_cpu

我们上一章说了实现内核同步的方法很多&#xff0c;如下表技术说明适用范围每CPU变量在CPU之间复制数据结构所有CPU原子操作对一个计数器原子地“读-修改-写”的指令所有CPU内存屏障避免指令重新排序本地CPU或所有CPU自旋锁加锁时忙等所有CPU信号量加锁时阻塞等待所有CPU顺序锁…

python的三维图片_python如何做三维图

Python三维绘图在遇到三维数据时&#xff0c;三维图像能给我们对数据带来更加深入地理解。python的matplotlib库就包含了丰富的三维绘图工具。1、创建三维坐标轴对象Axes3D创建Axes3D主要有两种方式&#xff0c;一种是利用关键字projection3dl来实现&#xff0c;另一种则是通过…

抽象类(Abstract)和接口的不同点、共同点(Interface)。

同样点&#xff1a; (1) 都能够被继承 (2) 都不能被实例化 (3) 都能够包括方法声明 (4) 派生类必须实现未实现的方法 区 别&#xff1a; (1) 抽象基类能够定义字段、属性、方法实现。接口仅仅能定义属性、索引器、事件、和方法声明&#xff0c;不能包括字…

为何要使用docker

可能很多人听说过docker&#xff0c;也可能有很多人用过&#xff0c;但是其中的一些细节&#xff0c;可能不是很清楚&#xff0c;还有一些人&#xff0c;像我一样&#xff0c;并不知道docker&#xff0c;也没有用过&#xff0c;刚好最近一个大神朋友比较有空&#xff0c;让他写…

如何解决文件不存在_传奇微端配置Pak密码文件不存在怎么解决?传奇分享汇

在架设gom引擎的版本时&#xff0c;你是否有遇到和我一样的情况呢&#xff1f;微端配置后pak密码文件不存在是怎么回事呢&#xff1f;今天分享pak密码文件不存在的解决方法为什么会出现pak密码文件不存在呢&#xff1f;总结分析有以下2种原因会导致文件不存在1、没有配置对应的…

linux 统计命令执行后的行数或者统计目录下文件数目

ls |wc 是统计你这个目录下的文件数目。ls |wc -l是输出第一个结果即31即文件的数目。 转载于:https://www.cnblogs.com/apple2016/p/6956814.html

oracle 删除补全日志组_Oracle 10g 添加、删除日志组

做日常巡检的时候发现alert日志中有这个错误Thread 1 cannot allocate new log, sequence 319708Checkpoint not complete这个实际上是个比较常见的错误。通常来说是因为在日志被写满时会切换日志组&#xff0c;这个时候会触发一次checkpoint&#xff0c;DBWR会把内存中的脏块往…

那些年,我们在?的那些日子

刚好在今天&#xff0c;我们几个比较好的朋友&#xff0c;都离开了一起奋斗的A公司。 先说明下&#xff0c;这个不是虚构的小说&#xff0c;也不是吹牛逼&#xff0c;就是记录我们几个曾经辉煌和落魄的日子&#xff0c;起名A公司也是为了保护大家的隐私&#xff0c;但是事情肯定…

db2 sql执行历史_5 个免费的在线 SQL 数据库环境,比Navicat 香!

来源&#xff1a;blog.csdn.net/horses/article/details/108603935作者&#xff1a;不剪发的Tony老师文章目录SQL FiddleDB Fiddledb<>fiddleSQL OnlineOracle Live SQL总结今天给大家分享几个在线的免费 SQL 运行环境&#xff0c;也就是在线数据库。这些网站可以帮助我们…

获取要素集中字段的唯一值

/// <summary> /// 获取要素集中字段的唯一值 /// </summary> /// <param name"featureClass">图层</param> /// <param name"fieldName">字段名称</param> /// <returns></returns> public static List&…

嵌入式入门必读

找到一个非常好的书籍而且不用购买的而且是高清版本的原来是放在我的知识星球里面的看到大家下载的也很多现在拿出来分享给大家从单片机到嵌入式这个不是一个简单的过程&#xff0c;其中从单进程到多进程&#xff0c;一个CPU如何做到多进程&#xff0c;怎么跑系统&#xff0c;调…

mysql远程访问 linux_Linux中开启mysql远程访问功能

1、确认3306是否对外开放&#xff0c;mysql默认状态下是不开放对外访问功能的。查看的办法如下&#xff1a;# netstat -an | grep 3306tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN从结果可以看出&#xff0c;mysql的3306端口只监听本地的连接&#xff0c;这样就阻碍了外部IP对该数…

致敬云南滑翔机

今天晚上&#xff0c;看了期待已久的篮球节目&#xff0c;我要打篮球&#xff0c;11点左右&#xff0c;感觉特别困&#xff0c;已经快睡着了&#xff0c;准备关掉电视的时候看到林书豪的图片&#xff0c;林书豪头上有一个标题《我要打篮球》&#xff0c;这个不就是《这&#xf…

个人编码规范

这里所列的编码规范都是我自己的一些编码习惯&#xff0c;同时参考了其他的一些编码规范而总结出的较常用一些规范。供参考&#xff1a; 1、一个类文件中只能包含一个类、枚举。2、所有public修饰的字段、属性、方法、类等必须作详细的注释。3、复杂的业务逻辑处应该注释清楚处…

震惊,用了这么多年的 CPU 利用率,其实是错的

来源&#xff1a;内核月谈, 原文链接&#xff1a;http://www.brendangregg.com/blog/2017-05-09/cpu-utilization-is-wrong.html本文中若有任何疏漏错误&#xff0c;责任在于编译者。有任何建议和意见&#xff0c;请回复内核月谈微信公众号&#xff0c;或通过 caspar at linux.…