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

2 Answers 2

2

Try this:

if firstTwoBytes != b'\xff\xd8':
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':

Comments

Your Answer

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.