open的正确使用

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

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