概述
我似乎得到了不同的输出:
from StringIO import *
file = open('1.bmp','r')
print file.read(),'\n'
print StringIO(file.read()).getvalue()
为什么?是因为StringIO只支持文本字符串或其他东西吗?
相反,尝试例如重新打开文件:
from StringIO import *
file = open('1.bmp','r')
print file.read(),'\n'
file.close()
file2 = open('1.bmp','r')
print StringIO(file2.read()).getvalue()
file2.close()
您还可以使用with语句使代码更清晰:
from StringIO import *
with open('1.bmp','r') as file:
print file.read(),'\n'
with open('1.bmp','r') as file2:
print StringIO(file2.read()).getvalue()
顺便说一句,我建议以二进制模式打开二进制文件:open(‘1.bmp’,’rb’)
总结
以上是编程之家为你收集整理的python – 带二进制文件的StringIO?全部内容,希望文章能够帮你解决python – 带二进制文件的StringIO?所遇到的程序开发问题。
如果您也喜欢它,动动您的小指点个赞吧