2013-05-01 60 views
0

使用子我使用子這樣如何,如果我在Python

args = ['commandname', 'some args'] 
subprocess.check_output(args) 

我可以顯示錯誤有時我得到這個錯誤

command returned non-zero exit status 1

有沒有什麼辦法,如果得到非零退出狀態,則系統通過該消息提出異常,如

output = subprocess.check_output(args) 
if non zero exit : 
    raise Exception(errormessage) 
+0

'subprocess.check_output' * *已經引發了一個異常與該消息,雖然。你想用你的命令輸出來提高一個嗎? – Cairnarvon 2013-05-01 01:19:08

+0

是的,我想顯示確切的錯誤信息 – user2330497 2013-05-01 01:24:59

回答

0

Y您可以同時使用subprocess屬性稱爲returncode[docs]

Popen.returncode 
    The child return code, set by poll() and wait() (and indirectly by communicate()). 
    A None value indicates that the process hasn’t terminated yet. 

    A negative value -N indicates that the child was terminated by signal N (Unix only). 

因此,它應該工作是這樣的(未測試代碼) -

import subprocess 
args = ['commandname', 'some args'] 
child = subprocess.Popen(args, stdout=subprocess.PIPE) 
streamdata = child.communicate()[0] 
returncode = child.returncode 
if returncode != 0: 
    raise Exception 
+0

thans哥們,那工作 – user2330497 2013-05-01 01:50:23