using struct module on a file

Ulrich Eckhardt doomster at
Wed Nov 18 17:42:11 CET 2009


I need to read a file containing packed "binary" data. For that, I find the 
struct module pretty convenient. What I always need to do is reading a chunk 
of data from the file (either using calcsize() or a struct.Struct instance) 
and then parsing it with unpack(). For that, I repeatedly write utility 
functions that help me do just that, but I can't imagine that there is no 
better way for that.

0. Am I approaching this from the wrong direction? I'm not a programming 
noob, but rather new to Python still.
1. The struct module has pack_into() or unpack_from(), could I somehow 
combine that with a file?
2. Is there some easier way to read files? I know about array and xdrlib, 
but neither really fit my desires.
3. Failing all that, would you consider this a useful addition to the struct 
module, i.e. should I write a feature request?



More information about the Python-list mailing list