【发布时间】:2015-10-31 03:06:58
【问题描述】:
标题说明一切。
我的代码:
try:
os.remove("NumPyResults.txt")
except IOError:
with open("NumPyResults.txt", 'a') as results_file:
outfile = csv.writer(results_file)
outfile.writerow(.......)
之所以在 append 中是因为它在一个函数中并且被多次调用。所以我每次运行程序时都想要一个新文件,删除旧文件并写入新文件。
但是,这不会创建新文件。我还在运行的目录中创建了该文件,它也没有删除它。
我明白了
WindowsError: [Error 2] The system cannot find the file specified: 'NumPyResults.txt'
【问题讨论】:
-
append不会根据定义删除文件 -
为什么不直接使用
open(path, 'w')?它会删除旧文件并重写它。 -
这一切都在一个函数里面?不是每次打电话都会删除文件吗?
标签: python file error-handling delete-file