【发布时间】:2019-12-08 20:24:08
【问题描述】:
我想从文件名以“df”开头的目录中删除所有文件。我曾经在下面的代码中做同样的事情,但我得到了下面的错误,说No such file or directory: 'df2.csv'although print 语句确实显示了 df2.csv
filelist = [ f for f in os.listdir("/a/b/") if f.startswith("df") ]
for f in filelist:
print(f)
os.remove(f)
df2.csv
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'df2.csv'
【问题讨论】:
-
f只是一个文件名,而不是一个路径。您要删除的文件位于os.path.join('/a/b', f)
标签: python python-3.x