gpt4 book ai didi

windows - 如何处理从 Python 中的子进程返回的负数?

转载 作者:可可西里 更新时间:2023-11-01 09:42:07 24 4
gpt4 key购买 nike

这段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;- 失败和错误是负数

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

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

编辑:最终代码有效这段代码的目的是运行许多简单的测试:

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 )

最佳答案

仅使用标准库:

>>> import struct
>>> struct.unpack('i', struct.pack('I', 4294966989))
(-307,)

关于windows - 如何处理从 Python 中的子进程返回的负数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27842166/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com