2014-01-13 22 views
0

我有以下Python代碼,我通過從python腳本內嵌入bash命令來調用Makefile。我想檢查一下警告是否在stdout上報告。我確實在stdout上看到了警告,但我的Python代碼似乎沒有檢測到它。無法正確解析Python的bash輸出

Python代碼:

maker = subprocess.Popen(["bash", "-c", "make"], stdout=subprocess.PIPE) 
    for line in maker.stdout: 
     if "warning:" in line: 
      print "Warning(s) detected in make"     

在標準輸出上輸出,明確報告警告:

main.c: In function ‘main’: 
main.c:46:14: warning: unused variable ‘options’ [-Wunused-variable] 
+2

編譯器錯誤消息會打印到標準錯誤,而不是標準輸出。 – Barmar

回答

4

嘗試捕捉標準錯誤,以及:

subprocess.Popen(["bash", "-c", "make"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) 

(作爲用戶 「Barmar」 已注意,編譯器錯誤消息被髮送到stderr。)

+0

@感謝邁克爾,這就是我的粗暴監督! –