2009-12-04 81 views
-2

我使用python來調用ant,我想獲取ant的返回碼來檢測ant的錯誤。Python:獲取窗口中的螞蟻子進程的返回碼

例如,輸入cmd.exe,

C:\Documents and Settings\Administrator>ant sfsf 
Buildfile: build.xml does not exist! 
Build failed 
C:\Documents and Settings\Administrator>echo %ERRORLEVEL% 
1 

但是在Python:

>>> a = subprocess.Popen("ant hahah",shell=True) 
>>> Buildfile: build.xml does not exist! 
Build failed 

>>> a.wait() 
0 

由於螞蟻是一個bat文件,所以我必須使用shell =真致電POPEN。 那麼如何獲得Python中的返回碼(1)?

編輯:我發現使用「呼叫螞蟻...」將解決這個問題,感謝您的答案。

回答

0

我的想法來解決這個問題: 獲得輸出流的一些正則表達式。如果典型的關鍵字出現「錯誤」,您將被通知。

0

作爲該交的建議的子模塊用於調用應用程序,因此,例如:

#Change to the correct project directory (with your build.xml) 
os.chdir(project_dir) 
print(os.path.abspath(os.curdir)) 
#debug is your ant task, Get the return code, (note shell=True can be a large security risk) 
output_return_code=subprocess.call(["ant","debug"],shell=True) 

#or to caputure the output 
output_text=subprocess.check_output(["ant","debug"],shell=True) 
print(str(output_text,"utf-8").strip()) 

Python的版本:3.2.1(默認情況下,2011年7月10日,20點02分51秒)[MSC v.1500 64位(AMD64)] Windows版本:Windows 7