title: Reading and interpreting data from a binary file in Python
body: <p>I want to read a file byte by byte and check if the last bit of each byte is set:</p>
  
  <pre><code>#!/usr/bin/python
  
  def main():
      fh = open('/tmp/test.txt', 'rb')
      try:
          byte = fh.read(1)
          while byte != "":
              if (int(byte,16) &amp; 0x01) is 0x01:
                  print 1
              else:
                  print 0
              byte = fh.read(1)
      finally:
          fh.close
  
      fh.close()
  
  if __name__ == "__main__":
          main()
  </code></pre>
  
  <p>The error I get is:</p>
  
  <pre><code>Traceback (most recent call last):
    File "./mini_01.py", line 21, in &lt;module&gt;
      main()
    File "./mini_01.py", line 10, in main
      if (int(byte,16) &amp; 0x01) is 0x01:
  ValueError: invalid literal for int() with base 16: '\xaf'
  </code></pre>
  
  <p>Anyone an idea? I didn't succeed using the struct and the binascii modules.</p>