[Tutor] curious struct problem

Alan Gauld alan.gauld at btinternet.com
Sat Feb 3 00:24:48 CET 2007

"Marcus Goldfish" <magoldfish at gmail.com> wrote

> interactively in the IPython shell, but not as a function invoked 
> from a
> command line (Windows).  Can someone point out my error?  Thanks!

One problem I dsee is that the function does not return anything.
It stores it in a local variable x which is garbage collected when
the function exits.

Could that be the problem?

It would help if you tell us exactly how it doesn't work.
Do you get an error message? If so what - exactly?
Is the data written anyplace? Is the format wrong?
What exactly is the problem we are trying to diagnose?
Without specific information we are just guessing.


Alan Gauld
Author of the Learn to Program web site

> import struct
> def demux(filename, channel, nchannels):
>   "Demultiplexes a stream of shorts (signed int16) from a file."
>   fmt = str(nchannels) + 'h'
>   sz = struct.calcsize(fmt)
>   infile = open(fname, 'rb')
>   chunk = infile.read(sz)
>   while chunk:
>      x = struct.unpack(fmt, chunk)
>      chunk = infile.read(sz)
>   infile.close()
> #struct.error: unpack str size does no match format


> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list