Unpacking byte strings from a file of unknown size

Mark mseagoe at gmail.com
Mon Oct 27 21:29:25 CET 2008


Hi;

I'm trying to use the struct.unpack to extract an int, int, char
struct info from a file.  I'm more accustomed to the file.readlines
which works well in a 'for' construct (ending loop after reaching
EOF).

# This does OK at fetching one 10-byte string at a time:
# (4, 4, 2 ascii chars representing hex)
info1, info2, info3 = struct.unpack('<IIH', myfile.read(10))

# Now to do the entire file, putting into a loop just gives error:
# TypeError: 'int' object is not iterable
for info1, info2, info3 in struct.unpack('<IIH', myfile.read(10)):

In trying to shoehorn this into a 'for' loop I've been unsuccessful.
I also tried other variations that also didn't work but no point
wasting space.  Using Python 2.5, WinXP

Thx,
Mark




More information about the Python-list mailing list