【发布时间】:2013-04-16 14:08:20
【问题描述】:
我想创建一个文件;如果它已经存在,我想删除它并重新创建它。我试过这样做,但它会引发 Win32 错误。我做错了什么?
try:
with open(os.path.expanduser('~') + '\Desktop\input.txt'):
os.remove(os.path.expanduser('~') + '\Desktop\input.txt')
f1 = open(os.path.expanduser('~') + '\Desktop\input.txt', 'a')
except IOError:
f1 = open(os.path.expanduser('~') + '\Desktop\input.txt', 'a')
【问题讨论】: