I read the first few bytes of a JPEG
f = open(filename, 'rb')
firstTwoBytes = f.read(2)
if firstTwoBytes != '\xff\xd8':
firstTwoBytes iny my debugger is: bytes: b'\xff\xd8' which is correct?
So my String comparison fails. How best to fix this?
Thanks
asked Jul 8, 2013 at 9:55
More Than Five
10.5k23 gold badges84 silver badges145 bronze badges
2 Answers 2
Try this:
if firstTwoBytes != b'\xff\xd8':
answered Jul 8, 2013 at 10:02
Harry.Chen
1,61012 silver badges12 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
More Than Five
Chan Thanks. As a matter of interest is this a python 3 thing? Is there any difference in behaviour here between python2 and python3?
So, compare to binary not to the string:
f = open(filename, 'rb')
firstTwoBytes = f.read(2)
if firstTwoBytes != b'\xff\xd8':
answered Jul 8, 2013 at 10:02
ElmoVanKielmo
11.4k2 gold badges35 silver badges51 bronze badges
Comments
lang-py