python中os.system.获取输出信息_python中os.system()的返回值

[python中os.system()的返回值]

如果第三方程序返回的是布尔型返回值,os.system会将true转为1,false转为0进行返回。

问题:

/bin/xxx.py是一个返回码为1的程序。

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

解决:

查阅了文档发现

os.system()的返回为:

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。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值