[Pythonmac-SIG] reading/writing binary data?

Joseph J. Strout joe@strout.net
Wed, 20 Jan 1999 15:18:11 -0800


I need to read a data file composed of integers in binary (Mac) format.  I
can't find anyway to do this -- it seems I can either open in text mode,
and read in strings, or open in binary mode, and read in raw characters.
That leaves me doing silly things like

def ReadInt4(file):
	bytes = file.read(4)
	return \
		(ord(bytes[0]) << 24) | 	\
		(ord(bytes[1]) << 16) | 	\
		(ord(bytes[2]) << 8) |	\
		(ord(bytes[3]))

...and, needless to say, this is  v e r y   s l o w  on large files.
Surely I'm missing the simple way to open a binary file and read in short
or long ints (or even whole lists of ints?) at once.  Could somebody please
point out what I'm missing?

Thanks,
-- Joe

,------------------------------------------------------------------.
|    Joseph J. Strout           developer: MacOS, Unix, 3D, AI     |
|    joe@strout.net             http://www.strout.net              |
`------------------------------------------------------------------'