open的正确使用

open一个对象的时候,不确定他是图片还是文本啊

#-----------------------
import io
 
with open('photo.jpg', 'rb') as inf:
    jpgdata = inf.read()
 
if jpgdata.startswith(b'\xff\xd8'):
    text = u'This is a JPEG file (%d bytes long)\n'
else:
    text = u'This is a random file (%d bytes long)\n'
 
with io.open('summary.txt', 'w', encoding='utf-8') as outf:
    outf.write(text % len(jpgdata))
 
#b是以二进制打开文件,图片的二进制内容是以 FF D8 开始
#io.open 可以指定文件的编码方式,utf-8等

posted on 2018-01-07 16:50  chungehpu  阅读(152)  评论(0编辑  收藏  举报

导航