当然这里换成open也是可以的,主要就是对字节流的文本对象进行一个包装,从而可以写入字符串。记住如果要想使用io.TextIOWrapper进行包装的话,那么打开的方式一定要是二进制的方式
python import io import gzip with open("1.png", "wb") as out: with io.TextIOWrapper(out, encoding="utf-8") as f: f.write("这是一张图片") # 读取文件 with open("1.png", "rb") as out: with io.TextIOWrapper(out, encoding="utf-8") as f: print(f.read()) # 这是一张图片 import os os.remove("1.png")
转载于:https://www.cnblogs.com/valorchang/p/11395485.html