pythoncook 文件和io

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')

扫描二维码关注公众号,回复: 4473961 查看本文章

2、字符串的io操作

io.StringIO() 创建类文件对象操作字符串

io.BytesIO() 创建类文件对象操作二进制数据

猜你喜欢

转载自www.cnblogs.com/charles7987/p/10107401.html