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

 

2、字符串的io操作

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

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

posted @ 2018-12-12 11:27  charles7987  阅读(206)  评论(0编辑  收藏  举报