使用 Windows/Linux 上的 python3 脚本检查可执行文件是 32 位还是 64 位
Checking if an executable is 32-bit or 64-bit with a python3 script on Windows/Linux
我正在 Python3 中编写软件(更具体地说:Python 3.8.1
)。在某些时候,软件需要检查某个任意可执行文件是 64 位还是 32 位。经过一番研究,我发现了以下 post:
Checking if an exe is 32 bit or 64 bit
在此post中,提供以下解决方案:
subprocess.call(['dumpbin', '/HEADERS', 'test2.exe', '|', 'find', '"machine"'])
不幸的是,这在 Python 3.8.1
中不起作用。 post 快 8 岁了,可以追溯到 Python 2.x
天。
如何从 Python 3.x
中测试 64 位?我需要 Linux 和 Windows 10.
的解决方案
EDITS :
Windows-related note:
Apparently the DumpBin
solution (see Checking if an exe is 32 bit or 64 bit post) requires Visual Studio to be installed. That's a no-no for me. My Python3 software should run on any Windows 10 computer.
Linux-related note:
On Linux, I don't neet to test PE format executables. Just Linux executables are fine.
检测 64 位 ELF 二进制文件(即 Linux)很容易,因为它总是在 header:
中的同一个位置
def is_64bit_elf(filename):
with open(filename, "rb") as f:
return f.read(5)[-1] == 2
我没有 Windows 系统,所以无法测试,但这可能适用于 Windows:
def is_64bit_pe(filename):
import win32file
return win32file.GetBinaryType(filename) == 6
我正在 Python3 中编写软件(更具体地说:Python 3.8.1
)。在某些时候,软件需要检查某个任意可执行文件是 64 位还是 32 位。经过一番研究,我发现了以下 post:
Checking if an exe is 32 bit or 64 bit
在此post中,提供以下解决方案:
subprocess.call(['dumpbin', '/HEADERS', 'test2.exe', '|', 'find', '"machine"'])
不幸的是,这在 Python 3.8.1
中不起作用。 post 快 8 岁了,可以追溯到 Python 2.x
天。
如何从 Python 3.x
中测试 64 位?我需要 Linux 和 Windows 10.
EDITS :
Windows-related note:
Apparently theDumpBin
solution (see Checking if an exe is 32 bit or 64 bit post) requires Visual Studio to be installed. That's a no-no for me. My Python3 software should run on any Windows 10 computer.Linux-related note:
On Linux, I don't neet to test PE format executables. Just Linux executables are fine.
检测 64 位 ELF 二进制文件(即 Linux)很容易,因为它总是在 header:
中的同一个位置def is_64bit_elf(filename):
with open(filename, "rb") as f:
return f.read(5)[-1] == 2
我没有 Windows 系统,所以无法测试,但这可能适用于 Windows:
def is_64bit_pe(filename):
import win32file
return win32file.GetBinaryType(filename) == 6