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。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM