如何处理返回码从Python中的subprocess负数? [英] How to deal with negative numbers that returncode get from subprocess in Python?
问题描述
这个脚本在python:
cmd ='installer.exe --install ...'#this works罚款,只是代表许多参数
process = subprocess.Popen(cmd)
process.wait()
print(process.returncode)
这个代码在我看来工作正常,问题是 .returncode
的值。
installer.exe确定,做了很多测试,现在我试图创建一个脚本在python自动化一个测试多天执行此installer.exe 。
installer.exe返回:
- 成功为0;
- 失败和错误是否定的数字
我有一个特定的错误,这是installer.exe返回-307。但是python当执行 print(process.returncode)
它显示4294966989 ...我如何处理负数在python,在这种情况下显示-307? p>
我是python的新用户,env是win7 32和python 3.4。
代码工作
这段代码的运行是运行很多简单的测试:
import子进程ctypes ,datetime,time
nIndex = 0
while 1 == 1:
cmd ='installer.exe --reinstall -nTHING NAME'
= 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)+'\\\
'
f = open('test_result.txt','a +')
f.write(输出)
f.closed
print
使用NumPy:查看无符号32位int, 4294966989,作为一个有符号的32位int:
在[39]:np.uint32(4294966989).view ')
Out [39]:-307
This piece of script in python:
cmd = 'installer.exe --install ...' #this works fine, the ... just represent many arguments
process = subprocess.Popen(cmd)
process.wait()
print(process.returncode)
This code works fine in my opinion, the problem is the value of .returncode
.
The installer.exe is ok, did many test to this, and now i trying to create a script in python to automate a test for many days executing this installer.exe .
The installer.exe return: - Success is 0; - Failure and errors are NEGATIVE numbers
I have a specific error that is -307 that installer.exe return. But python when execute print(process.returncode)
its shows 4294966989 ... How can i deal with negative numbers in python, to show in this case the -307?
I am new to python and the env is win7 32 and python 3.4.
EDIT: the final code working The porpose of this code is to run many simple test:
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 )
Using NumPy: view the unsigned 32-bit int, 4294966989, as a signed 32-bit int:
In [39]: np.uint32(4294966989).view('int32')
Out[39]: -307
这篇关于如何处理返回码从Python中的subprocess负数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!