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?
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', '')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With