I&#39;m trying to read a binary file using struct.&nbsp; This code works interactively in the IPython shell, but not as a function invoked from a command line (Windows).&nbsp; Can someone point out my error?&nbsp; Thanks!<br><br>import struct 
<br><br>def demux(filename, channel, nchannels):<br>&nbsp;&nbsp; &quot;Demultiplexes a stream of shorts (signed int16) from a file.&quot;<br>&nbsp;&nbsp; fmt = str(nchannels) + &#39;h&#39;<br>&nbsp;&nbsp; sz = struct.calcsize(fmt)<br><br>&nbsp;&nbsp; infile = open(fname, &#39;rb&#39;)
<br>&nbsp;&nbsp; chunk = infile.read(sz)<br>&nbsp;&nbsp; while chunk:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x = struct.unpack(fmt, chunk)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; chunk = infile.read(sz)<br>&nbsp;&nbsp; infile.close()<br><br>#struct.error: unpack str size does no match format<br><br>