今天学习IO模块的使用,主要分为StringIO的使用和BytesIO的使用~
# 类似文件的缓冲区
from io import StringIO
cache_file = StringIO()
print(cache_file.write('hello world')) # 11
print(cache_file.seek(0)) # 0
print(cache_file.read()) # hello world
print(cache_file.close()) # 释放缓冲区
# 类似文件的缓冲区
from io import BytesIO
bytes_file = BytesIO()
bytes_file.write(b'hello world')
bytes_file.seek(0)
print(bytes_file.read()) # b'hello world'
bytes_file.close()
Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号