ByteIO
与StringIO
处理数据是在内存中完成的,需要用到的包是io
二进制文件ByteIO
import io
file = open("test_pdf.pdf", 'br') # 使用二进制
io_file = io.BytesIO(file.read()) # 使用BytesIO读取
print(io_file.read())
# 写入BytesIO
with open('test_pdf_2.pdf', 'wb') as file:
file.write(io_file.getvalue()) # 保存到本地
普通的文本文件StringIO
import io
file = open("test.txt", 'r')
io_file = io.StringIO(file.read()) # 使用StringIO读取
print(io_file.read())
# 写入StringIO
with open("test_2.txt", 'w') as file:
file.write(io_file.getvalue())