[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。