的Python:获得窗口蚁子过程的返回code [英] Python: get the return code of ant sub-process in windows
问题描述
我用Python来调用Ant,我想要得到的蚂蚁返回code的检测蚂蚁错误。
I use python to call ant, I want to get the return code of the ant for detect ant error.
例如,输入cmd.exe。
for example, in 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:
but in python:
>>> a = subprocess.Popen("ant hahah",shell=True)
>>> Buildfile: build.xml does not exist!
Build failed
>>> a.wait()
0
由于蚂蚁是一个bat文件,所以我必须使用shell =真致电POPEN。
所以,我怎么能得到在Python返回code(1)?
Because ant is a bat file, so I must use shell=True to call the Popen. So how can I get the return code (1) in Python?
修改:我发现使用呼蚂蚁......将解决这个问题,谢谢你的答案
EDIT: I found use "call ant ..." will solve this problem, thanks for the answers.
推荐答案
由于这个职位的建议,该子模块用来调用应用程序,因此,例如:
As of this post it is recommended that the subprocess module be used to call applications, so for example:
#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的
Python Version: 3.2.1 (default, Jul 10 2011, 20:02:51) [MSC v.1500 64 bit (AMD64)] Windows Version: Windows 7
这篇关于的Python:获得窗口蚁子过程的返回code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!