我有一个文件夹,其中包含一个地理数据库和两个其他文件 (txt)。我使用了 zip 并压缩了它们。所以现在在这个文件夹中我有 gdb、txt、txt 和新的 zip 文件。现在我需要删除那些被压缩的文件,这样文件夹中就只有压缩文件了。 我写了下面的代码:
def remove_files():
for l in os.listdir(DestPath):
if l.find('zipped.zip') > -1:
pass
else:
print ('Deleting ' + l)
os.remove(l)
但是得到了:
Error Info:
[Error 2] The system cannot find the file specified: 'geogeo.gdb'
谁能帮帮我? 提前谢谢你。
最佳答案
os.listdir
只返回文件名,不返回完整路径。
如果只给出文件名,os.remove
使用当前工作目录。如果当前工作目录不同于 DestPath
,则您需要提供完整路径:
os.remove(os.path.join(DestPath,l))
关于python - 使用python删除文件夹中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10580715/