每天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!')
import os
if not os.path.exists('somefile'):
with open('somefile', 'wt') as f:
f.write('Hello\n')
else:
print('File already exists!')