using struct module on a file

Jon Clements joncle at
Wed Nov 18 18:12:14 CET 2009

On Nov 18, 4:42 pm, Ulrich Eckhardt <dooms... at> wrote:
> Hia!
> 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.
> Questions:
> 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?
> Thanks!
> Uli

First time I've seen zero based indexing for paragraph markers :)

unpack_from() will work on anything that supports the buffer

To work with files you can use something like:

my4chars = struct.Struct('4c')
def struct_read(s, f):
  return s.unpack_from(

Which isn't hideously painful.


More information about the Python-list mailing list