title: Python struct.unpack not working
body: <p>I'm trying to run this:</p>
  
  <pre><code>def ReadWord(fid,fmt,Addr):
      fid.seek(Addr)
      s = fid.readline(2)
      s = unpack(fmt + 'h', s)
      if(type(s) == tuple):
          return s[0]
      else:
          return s    
  </code></pre>
  
  <p>with:</p>
  
  <pre><code>len(s) = 2
  len(fmt) = 1
  calcsize(fmt) = 0
  calcsize(fmt + 'h') = 2
  </code></pre>
  
  <p>However, Python returns: </p>
  
  <blockquote>
    <p>struct.error: unpack requires a string argument of length 4</p>
  </blockquote>
  
  <p>According to python <code>struct.unpack</code> <a href="https://docs.python.org/2/library/struct.html#struct.unpack" rel="nofollow">documentation</a> : </p>
  
  <blockquote>
    <p>The string must contain exactly the amount of data required by the format (len(string) must equal calcsize(fmt)).</p>
  </blockquote>
  
  <p>So if the length of my string is 2 and calcsize of <code>fmt+'h'</code> is also 2, why does python say "unpack requires a string argument of length <strong>4</strong>" ??</p>
  
  <p><strong>EDIT :</strong> </p>
  
  <p>Thanks for all your answers. Here is the full code:</p>
  
  <p><a href="http://qtwork.tudelft.nl/gitdata/users/guen/qtlabanalysis/analysis_modules/general/lecroy.py" rel="nofollow">http://qtwork.tudelft.nl/gitdata/users/guen/qtlabanalysis/analysis_modules/general/lecroy.py</a></p>
  
  <p>So as you can see in the <code>read_timetrace</code> function, <code>fmt</code> is set to <code>'&lt;'</code> or <code>'&gt;'</code> in a <code>if...else</code> statement. Printing it confirmes that.</p>
  
  <p><strong>But</strong> you should also know that I'm working on windowsx64 (for work).</p>
  
  <p><strong>EDIT2</strong></p>
  
  <p>Here's the full traceback, sorry for the mistake.</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Users\maxime.vast\Desktop\Test Campaign Template\Test Suite\Include\readLecroyTRCFile.py", line 139, in &lt;module&gt;
      read_timetrace("C:\Users\maxime.vast\Desktop\Test Campaign Template\Test Suite\Traces\KL.ES.001.001.trc")
    File "C:\Users\maxime.vast\Desktop\Test Campaign Template\Test Suite\Include\readLecroyTRCFile.py", line 60, in read_timetrace
      WAVE_ARRAY_1        = ReadLong(fid, fmt, aWAVE_ARRAY_1)
    File "C:\Users\maxime.vast\Desktop\Test Campaign Template\Test Suite\Include\readLecroyTRCFile.py", line 100, in ReadLong
      s = unpack(fmt + 'l', s)
  struct.error: unpack requires a string argument of length 4
  [Finished in 0.2s]
  </code></pre>
  
  <p><strong>EDIT3:</strong></p>
  
  <p>I replaced <code>readline</code> by <code>read</code> and add : </p>
  
  <pre><code>print "len(s) ", len(s)
  print "len(fmt) ", len(fmt)
  print "calcsize(fmt) ", calcsize(fmt)
  print "calcsize(fmt + 'h') ", calcsize(fmt + 'h')
  print "fmt ", fmt
  </code></pre>
  
  <p>to <code>ReadLong</code> function.</p>
  
  <p>Here's the new traceback : </p>
  
  <pre><code>len(s)  4
  len(fmt)  1
  calcsize(fmt)  0
  calcsize(fmt + 'h')  2
  fmt  &lt;
  len(s)  4
  len(fmt)  1
  calcsize(fmt)  0
  calcsize(fmt + 'h')  2
  fmt  &lt;
  len(s)  4
  len(fmt)  1
  calcsize(fmt)  0
  calcsize(fmt + 'h')  2
  fmt  &lt;
  len(s)  1
  len(fmt)  1
  calcsize(fmt)  0
  calcsize(fmt + 'h')  2
  fmt  &lt;
  Traceback (most recent call last):
    File "C:\Users\maxime.vast\Desktop\Test Campaign Template\Test Suite\Include\readLecroyTRCFile.py", line 143, in &lt;module&gt;
      read_timetrace("C:\Users\maxime.vast\Desktop\Test Campaign Template\Test Suite\Traces\KL.ES.001.001.trc")
    File "C:\Users\maxime.vast\Desktop\Test Campaign Template\Test Suite\Include\readLecroyTRCFile.py", line 60, in read_timetrace
      WAVE_ARRAY_1        = ReadLong(fid, fmt, aWAVE_ARRAY_1)
    File "C:\Users\maxime.vast\Desktop\Test Campaign Template\Test Suite\Include\readLecroyTRCFile.py", line 104, in ReadLong
      s = unpack(fmt + 'l', s)
  struct.error: unpack requires a string argument of length 4
  [Finished in 0.2s]
  </code></pre>