JStroop
JStroop

Reputation: 475

Python - Read string from binary file

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 'colr' (this is a JPEG 2000 file) and here is what I have so far:

from collections import deque

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

if this works:

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

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

Upvotes: 3

Views: 2671

Answers (2)

martineau
martineau

Reputation: 123463

Change your bytes.appendleft() to bytes.append() and then it will work -- it does for me.

Upvotes: 2

Joran Beasley
Joran Beasley

Reputation: 113978

  with open("my.jpg","rb") as f:
       print f.read().split("colr",1)

if you dont want to read it all at once ... then

def preprocess(line):
    print "Do Something with this line"
def postprocess(line):
    print "Do something else with this line"
currentproc = preprocess
with open("my.jpg","rb") as f:
   for line in f:
       if "colr" in line:
           left,right = line.split("colr")
           preprocess(left)
           postprocess(right) 
           currentproc= postprocess
        else:
           currentproc(line)

its line by line rather than byte by byte ... but meh ... I have a hard time thinking that you dont have enough ram to hold the whole jpg in memory... python is not really an awesome language to minimize memory or time footprints but it is awesome for functional requirements :)

Upvotes: 0

Related Questions