Skip to content Skip to sidebar Skip to footer

Checking If An Executable Is 32-bit Or 64-bit With A Python3 Script On Windows/Linux

I'm writing software in Python3 (more specifically: Python 3.8.1). At some point, the software needs to check if some arbitrary executable is 64-bit or 32-bit. After some research,

Solution 1:

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

Post a Comment for "Checking If An Executable Is 32-bit Or 64-bit With A Python3 Script On Windows/Linux"