inux上使用python3脚本检查可执行文件是32位还是64位

inux上使用python3脚本检查可执行文件是32位还是64位

本文介绍了在Windows / Linux上使用python3脚本检查可执行文件是32位还是64位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Python3编写软件(更具体地说: Python 3.8.1 )。在某个时候,软件需要检查某些任意可执行文件是64位还是32位。经过研究,我发现了以下信息:





在本文中,以下解决方案是提供:

  subprocess.call(['dumpbin','/ HEADERS','test2。 exe','|','find',' machine'])

不幸的是,这在 Python 3.8.1 中不起作用。该帖子将近8年了,其历史可以追溯到 Python 2.x 天。



我如何测试从 Python 3.x 中获得64位?我需要适用于Linux和Windows 10的解决方案。

解决方案

Detecting the 64-bitness of ELF binaries (i.e. Linux) is easy, because it's always at the same place in the header:

def is_64bit_elf(filename):
    with open(filename, "rb") as f:
        return f.read(5)[-1] == 2

I don't have a Windows system, so I can't test this, but this might work on Windows:

def is_64bit_pe(filename):
    import win32file
    return win32file.GetBinaryType(filename) == 6

这篇关于在Windows / Linux上使用python3脚本检查可执行文件是32位还是64位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 00:52