[Tutor] Read-ahead for large fixed-width binary files?

Kent Johnson kent37 at tds.net
Sun Nov 18 19:28:35 CET 2007


Marc Tompkins wrote:
> though - I think I need to do
> 
>     if len(buf) < recLen:
>             return
> 
> rather than
> 
>           if not buf:
>             return
> 
> I'll have to experiment again to refresh my memory, but I believe I 
> tried that in one of my first iterations (about a year ago, so I may be 
> remembering wrong.)  If I remember correctly, read() was still returning 
> a result - but with a size that didn't evaluate to false.  As you can 
> imagine, hilarity ensued when I tried to slice the last record.

Yes, there might be a partial record or a newline or some kind of cruft 
at the end of the file. If that is the case you will have to guard 
against yielding short records, too, e.g.

rec = buf[ix:ix+recLen]
if len(rec) == recLen:
   yield rec

which will make my version about as long as yours I think :-)

Kent


More information about the Tutor mailing list