Unpacking byte strings from a file of unknown size

Terry Reedy tjreedy at udel.edu
Mon Oct 27 18:18:24 EDT 2008


Mark wrote:
> 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).

You do not need .readlines to iterate through a file by lines.
   for line in f.readlines():pass
is awkward if you have 100million 100 byte lines, whereas
   for line in f: pass
will read one line at a time and process before reading the next.

> 
> # 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.

for loops require an iterator.  Files only come with one.  So either use 
a while loop or define a reusable file-block generator (untested):

def blocks(openfile, n):
   while True:
     block = openfile.read(n)
     if len(block) == n:
       yield block
     else:
       raise StopIteration

Terry Jan Reedy




More information about the Python-list mailing list