0

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

2 Answers 2

2

Try this:

if firstTwoBytes != b'\xff\xd8':
answered Jul 8, 2013 at 10:02
Sign up to request clarification or add additional context in comments.

1 Comment

Chan Thanks. As a matter of interest is this a python 3 thing? Is there any difference in behaviour here between python2 and python3?
1

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.