Python - Read string from binary file
<p>I need to read up to the point of a certain string in a binary file, and then act on the bytes that follow. The string is <code>'colr'</code> (this is a JPEG 2000 file) and here is what I have so far:</p>

<pre><code>from collections import deque

f = open('my.jp2', 'rb')
bytes =  deque([], 4)
while ''.join(map(chr, bytes)) != 'colr':
    bytes.appendleft(ord(f.read(1)))
</code></pre>

<p>if this works:</p>

<pre><code>bytes =  deque([0x63, 0x6F, 0x6C, 0x72], 4)
print ''.join(map(chr, bytes))
</code></pre>

<p>(returns 'colr'), I'm not sure why the test in my loop never evaluates to <code>True</code>. I wind up spinning - just hanging - I don't even get an exit when I've read through the whole file.</p>
