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

标签 windows python-3.x cmd subprocess exit-code

这段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/

相关文章:

python - 为什么我不能从Windows上的pip下载我的软件包的最新版本?

java - 作为 Windows 服务运行的 Java 应用程序中 STDOUT 存储的位置

Python - 有没有办法将 "as"别名添加到 for 循环?

python - 从 opentdb 请求时出现 SSLError

java - 在 java(w).exe 进程列表中查找 .jar 的名称

windows - 如何将参数或变量从一个 powershell 脚本传递到另一个?

Python 在 64 位 Vista 上获得 os.environ ["ProgramFiles"] 的错误值

windows - NSIS $TEMP 有不同的值(value)

python - 使用 python 3 在终端中打印 unicode 字符

windows - 您可以为 Windows CMD 命令设置键盘快捷键吗?