python - 无法删除特定扩展名的文件

标签 python file python-3.x operating-system delete-file

我正在尝试删除文件夹中的一些文件。

这是我为此编写的内容:

import sys
import os
from os import listdir
from os.path import join

dir_path = os.path.dirname(os.path.realpath(__file__))

for file in dir_path:
    if (file.endswith(".gz")) or (file.endswith(".bz2")):
        os.remove(join((dir_path), file))
        print("Removed file.")

print("Done.")

当我运行该模块时,它只打印“完成”。但不删除任何文件,即使在与模块相同的目录中存在具有该扩展名的文件。

无法弄清楚我做错了什么,帮忙?

最佳答案

看起来您在 for 循环中错过了 os.listdir(dir_path)

关于python - 无法删除特定扩展名的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41235138/

相关文章:

file - 加载项目时出错,无法加载模块intellij idea

python-3.x - AttributeError : Cannot access callable attribute 'reset_index' of 'DataFrameGroupBy' objects, 尝试使用 'apply' 方法

python - 如何使用 -c 选项执行多行 python

mysql - 在处理 `except` 的 `ProgrammingError` 语句后仍然收到错误消息

c - 使用 argc 和 argv 读取文件

python - 当我想检查 Dataframe 是否为空时,“list”对象没有属性 'isEmpty'

python - 从Python中的16位无符号大端原始图像文件读取数据

python - 为什么计算时差这么大?

python - 是否有 Yandex 的搜索 API,最好是在 python 中?

c - 在C语言中,如何打开不指定扩展名的文件?