Read header and data from a binary file

MRAB python at mrabarnett.plus.com
Tue Sep 22 18:15:02 EDT 2009


Jose Rafael Pacheco wrote:
> Hello,
> 
> I want to read from a binary file called myaudio.dat
> Then I've tried the next code:
> 
> import struct
> name = "myaudio.dat"
> f = open(name,'rb')
> f.seek(0)
> chain = "< 4s 4s I 4s I 20s I I i 4s I 67s s 4s I"
> s = f.read(4*1+4*1+4*1+4*1+4*1+20*1+4*1+4*1+4*1+4*1+4*1+67*1+1+4*1+4*1)
[snip]
FYI, the struct module has a function called 'calcsize', so:

     s = f.read(struct.calcsize(chain))



More information about the Python-list mailing list