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.

There is, however, a well defined concept for "address size", which the accepted answer reports, but probably has nothing at all to do with data that could be found in a file (unless something in /sys or /proc on linux systems with sysfs/procfs exposes binary address values)
–
IfLoopAug 3 '11 at 5:09

5 Answers
5

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:

There's no really sound definition for what a word is; except that certain archetectures call some number of bytes 'word' (x86 calls 2 bytes a word, PPC calls 4 bytes a word), but there's not much significance besides this arbitrary value.

Perhaps the simplest solution is to just defer to the struct module; for instance, the format 'h' means signed short (which reasonably agrees with the intel definition of 'word'). So you could do this:

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

Then you need to ask the person who wrote the file. It has nothing to do with Python and everything to do with what the actual file format is. It's pretty odd for a file to be defined as a sequence of words BTW. It is most probably a sequence of 16- or 32-bit integers, or else it really is words in the text sense, in which case you are really scanning the file for tokens between whatever the delimiters are.