How to determine 'word' size in Python

后端 未结 6 472
再見小時候
再見小時候 2021-01-20 00:03

I need to know the number of bytes in a \'word\' in Python. The reason I need this is I have the number of words I need to read from a file; if I knew the number of bytes in

6条回答
  •  情歌与酒
    2021-01-20 00:21

    You can use the platform.architecture function:

    >>> import platform
    >>> platform.architecture()
    ('64bit', '')
    

    Pay attention to the note on the same page:

    Note On Mac OS X (and perhaps other platforms), executable files may be universal files containing multiple architectures. To get at the “64-bitness” of the current interpreter, it is more reliable to query the sys.maxsize attribute:

    is_64bits = sys.maxsize > 2**32
    

    Please keep in mind that this gives the word size with which the python interpreter was compiled. You could obtain a value of 32 on a 64bit host if python was compiled in 32bit mode.

    If the file is produced by a different executable and you have access to this executable, you can use the first optional argument to the platform.architecture function:

    >>> p.architecture('/path/to/executable')
    ('32bit', '')
    

提交回复
热议问题