title: Python: Read and write binary data
body: <p>I am aware that there are a lot of almost identical questions, but non seems to really target the general case.</p>
  
  <p>So assume I want to open a file, read it in memory, possibly do some operations on the respective bitstring and write the result back to file.</p>
  
  <p>The following is what seems straightforward to me, but it results in completely different output. Note that for simplicity I only copy the file here:</p>
  
  <pre><code>file = open('INPUT','rb')
  data = file.read()
  data_16 = data.encode('hex')
  data_2 = bin(int(data_16,16))
  
  OUT = open('OUTPUT','wb')
  
  i = 0
  while i &lt; len(data_2) / 8:
      byte = int(data_2[i*8 : (i+1)*8], 2)
      OUT.write('%c' % byte)
      i += 1
  
  OUT.close()
  </code></pre>
  
  <p>I looked at <code>data</code>, <code>data_16</code> and <code>data_2</code>. The transformations make sense as far as I can see.</p>
  
  <p>As expected, the output file has exactly the same size in bits as the input file.</p>
  
  <p>EDIT: I considered the possibility that the leading '0b' has to be cut. See the following:</p>
  
  <pre><code>&gt;&gt;&gt; data[:100]
  'BMFU"\x00\x00\x00\x00\x006\x00\x00\x00(\x00\x00\x00\xe8\x03\x00\x00\xee\x02\x00\x00\x01\x00\x18\x00\x00\x00\x00\x00\x00\x00\x00\x00\x12\x0b\x00\x00\x12\x0b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05=o\xce\xf4^\x16\xe0\x80\x92\x00\x00\x00\x01I\x02\x1d\xb5\x81\xcaN\xcb\xb8\x91\xc3\xc6T\xef\xcb\xe1j\x06\xc3;\x0c*\xb9Q\xbc\xff\xf6\xff\xff\xf7\xed\xdf'
  &gt;&gt;&gt; data_16[:100]
  '424d46552200000000003600000028000000e8030000ee020000010018000000000000000000120b0000120b000000000000'
  &gt;&gt;&gt; data_2[:100]
  '0b10000100100110101000110010101010010001000000000000000000000000000000000000000000011011000000000000'
  &gt;&gt;&gt; data_2[1]
  'b'
  </code></pre>
  
  <p>Maybe the <code>BMFU"</code> part should be cut from <code>data</code>?</p>