Python:获取windows中ant子进程的返回码 [英] Python: get the return code of ant sub-process in windows

查看:27
本文介绍了Python:获取windows中ant子进程的返回码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用python调用ant,我想得到ant的返回码,用于检测ant错误.

例如在cmd.exe中,

C:\Documents and Settings\Administrator>ant sfsf构建文件:build.xml 不存在!构建失败C:\Documents and Settings\Administrator>echo %ERRORLEVEL%1

但在 python 中:

<预><代码>>>>a = subprocess.Popen("蚂蚁哈哈",shell=True)>>>构建文件:build.xml 不存在!构建失败>>>等待()0

因为ant是bat文件,所以我必须使用shell=True来调用Popen.那么如何在 Python 中获取返回码 (1)?

编辑:我发现使用call ant ..."可以解决这个问题,感谢您的回答.

解决方案

截至本文,建议使用 subprocess 模块来调用应用程序,例如:

#切换到正确的项目目录(用你的build.xml)os.chdir(project_dir)打印(os.path.abspath(os.curdir))#debug 是你的蚂蚁任务,获取返回码,(注意 shell=True 可能存在很大的安全风险)output_return_code=subprocess.call(["ant","debug"],shell=True)#或捕获输出output_text=subprocess.check_output(["ant","debug"],shell=True)打印(str(output_text,"utf-8").strip())

Python 版本:3.2.1(默认,2011 年 7 月 10 日,20:02:51)[MSC v.1500 64 位 (AMD64)]Windows 版本:Windows 7

I use python to call ant, I want to get the return code of the ant for detect ant error.

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

but in python:

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

>>> a.wait() 
0

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 Version: 3.2.1 (default, Jul 10 2011, 20:02:51) [MSC v.1500 64 bit (AMD64)] Windows Version: Windows 7

这篇关于Python:获取windows中ant子进程的返回码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆