Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine 'word' size in Python

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 a word, I can use the file.read(num_bytes) function to read the appropriate amount from the file.

How can I determine the number of bytes in a word?

like image 564
jlconlin Avatar asked Feb 24 '23 09:02

jlconlin


1 Answers

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', '')
like image 89
GaretJax Avatar answered Mar 03 '23 07:03

GaretJax