ANN: binario - simple work with binary files

Tim Roberts timr at probo.com
Mon Sep 1 20:35:19 EDT 2014


Rustom Mody <rustompmody at gmail.com> wrote:

>On Tuesday, August 26, 2014 6:58:42 AM UTC+5:30, Tim Roberts wrote:

>> To the equivalent code with struct:
>
>>   import struct
>
>>   dscrp = "H?fs5B"
>
>>   f = open('file.dat')
>>   stuff = struct.unpack( dscrp, f.read() )
>
>>   print stuff
>
>> In both cases, you have to KNOW the format of the data beforehand.  If you
>> do a read_short where you happen to have written a float, disaster ensues.
>
>> I don't really see that you've added very much.
>
>I thought much the same.
>However notice your f.read(). Its type is string.
>
>What if file.dat is a 1GB wav file?

  f.seek(512000)
  stuff = struct.unpack( dscrp, f.read(128) )

The point is that files already know how to position themselves.
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list