每天CookBook之Python-077

  • 写入文件
  • 当文件不存在时创建文件
import os

if not os.path.exists('somefile'):
    with open('somefile', 'wt') as f:
        f.write('Hello\n')
else:
    print('File already exists!')
posted @ 2016-07-24 12:51  4Thing  阅读(147)  评论(0编辑  收藏  举报