如何处理返回码从 Python 中的子进程获得的负数? [英] How to deal with negative numbers that returncode get from subprocess in Python?

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

问题描述

python中的这段脚本:

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)

我认为这段代码运行良好,问题在于 .returncode 的值.

This code works fine in my opinion, the problem is the value of .returncode.

installer.exe 没问题,对此做了很多测试,现在我尝试在 python 中创建一个脚本来自动化测试很多天,执行这个 installer.exe.

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 .

installer.exe 返回:- 成功为0;- 失败和错误是负数

The installer.exe return: - Success is 0; - Failure and errors are NEGATIVE numbers

我有一个特定的错误,即 installer.exe 返回的 -307.但是 python 在执行 print(process.returncode) 时显示 4294966989 ...我如何处理 python 中的负数,在这种情况下显示 -307?

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?

我是python新手,环境是win7 32和python 3.4.

I am new to python and the env is win7 32 and python 3.4.

最终代码工作这段代码的目的是运行许多简单的测试:

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 ) + '
'

   f = open( 'test_result.txt', 'a+' )
   f.write( output )
   f.closed

   print( output )

推荐答案

使用 NumPy:查看无符号的 32 位 int,4294966989,作为有符号的 32 位 int:

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 中的子进程获得的负数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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