I seem to get different outputs:
from StringIO import *
file = open('1.bmp', 'r')
print file.read(), '\n'
print StringIO(file.read()).getvalue()
Why? Is it because StringIO only supports text strings or something?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When you call
file.read(), it will read the entire file into memory. Then, if you callfile.read()again on the same file object, it will already have reached the end of the file, so it will only return an empty string.Instead, try e.g. reopening the file:
You can also use the
withstatement to make that code cleaner:As an aside, I would recommend opening binary files in binary mode:
open('1.bmp', 'rb')