On Tuesday, September 2, 2014 6:05:19 AM UTC+5:30, Tim Roberts wrote: > Rustom Mody 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) ) And what if the struct you are (trying to) unpack is greater or less than 128 bytes? > The point is that files already know how to position themselves. Sure. But its not enough. One can write a generator that yields one char at a time. How to feed that to struct.unpack?? -- https://mail.python.org/mailman/listinfo/python-list