这一段脚本在python:如何处理从Python中的子进程返回的负数?
cmd = 'installer.exe --install ...' #this works fine, the ... just represent many arguments
process = subprocess.Popen(cmd)
process.wait()
print(process.returncode)
此代码的工作在我看来不错,问题是.returncode
值。
installer.exe没问题,对此做过很多测试,现在我试图在python中创建一个脚本来自动执行测试,执行installer.exe多日。
installer.exe返回: - 成功为0; - 失败和错误是负数
我有一个特定的错误是-307 installer.exe返回。但是,执行时的python print(process.returncode)
它的显示4294966989 ...我如何处理python中的负数,在这种情况下显示-307?
我是python的新手,env是win7 32和python 3.4。
编辑:最终代码工作 此代码的porpose是运行许多简单的测试:
import subprocess, ctypes, datetime, time
nIndex = 0
while 1==1:
cmd = 'installer.exe --reinstall -n "THING NAME"'
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
now = datetime.datetime.now()
ret = ctypes.c_int32(process.wait()).value
nIndex = nIndex + 1
output = str(now) + ' - ' + str(nIndex) + ' - ' + 'Ret: ' + str(ret) + '\n'
f = open('test_result.txt', 'a+')
f.write(output)
f.closed
print(output)
请注意,Windows退出代码被定义为32位*无符号*整数,所以Python实际上在这里做了正确的事情。 –
无关:使用'returncode = subprocess.call(cmd)'而不是'Popen(cmd).wait()'。除非从管道读取数据,否则不要使用'stdout = PIPE'。如果要放弃输出,请使用'stdout = subprocess.DEVNULL'。打印('{now} - {nIndex} - Ret:{ret}'格式,而不是多个'str()'调用, (** vars()),file = file)' – jfs
不要把解决方案放到问题中。将其作为回答代替 – jfs