1、文件不存在,则写入;文件存在则,报错
try:
with open('file','x') as f:
f.write()
except FileExistsError:
print('file exists')
相当于:if not os.path.exists('file'):
with open('file','w') as f :
f.write('')
else:
print('file exists')
2、字符串的io操作
io.StringIO() 创建类文件对象操作字符串
io.BytesIO() 创建类文件对象操作二进制数据