How to efficiently read binary files?

David Lees debl2NoSpam at verizon.net
Sun Apr 30 21:31:15 EDT 2006


Alex Martelli wrote:
> David Lees <debl2NoSpam at verizon.net> wrote:
> 
>> I want to process large binary files (>2GB) in Python.  I have played
>> around with prototypes in pure Python and profiled the code.  Most of
>> the time seems to be spent converting back and forth to and from strings
>> using the struct module.  Is there a way to directly read into an array
>> of integers in Python?
> 
>>>> import array
>>>> x=array.array('l')
>>>> help(x.fromfile)
> 
> Help on built-in function fromfile:
> 
> fromfile(...)
>     fromfile(f, n)
>     
>     Read n objects from the file object f and append them to the end of
>     the array.  Also called as read.
> 
> 
> Alex

Thank you.  This is exactly what I was looking for.  I just tried it and 
it works great.

David Lees



More information about the Python-list mailing list