python - 删除文件夹和所有子文件夹中python中的文件类型
问题描述
我正在用 Python 编写脚本,需要清理才能在最后运行。我需要做的是删除任何 .ZIP 文件
我使用以下脚本在第一个文件夹上实现了这一点。
print("Deleting all .ZIP files")
dir_name = os.path.expanduser("~") + "/AWSSplunk/Downloads/"
test = os.listdir(dir_name)
for item in test:
if item.endswith(".zip"):
os.remove(os.path.join(dir_name, item))
print("Deleting .ZIP files completed")
但是,我还需要删除其他子目录中的这些 .ZIP 文件 - 我怎样才能让它逐步通过该位置的子目录删除这些文件。
解决方案
使用glob
模块:
import glob
for file in glob.glob("**/*.zip", recursive=True):
# delete file
推荐阅读
- python - 无法获取 Azure 认知服务的访问令牌(用于 tts)
- notepad++ - 如果至少有两个或多个大写字母我想将它们更改为小写
- regex - 在python中查找字符串中每个唯一字符的开始和结束索引
- javascript - 添加 javascript 后 prestashop 1.7.5.2 购物篮出现问题
- r - 在闪亮的应用程序中突出显示特定的直方图条
- c++ - 自定义类型(自己的类)的 std::rank 的实现
- java - 使用 MongoRepository 在 springBoot 中查询 MongoDb
- jquery - 在 Controller Asp.Net Mvc 和 Jquery Ajax 之间加密数据
- python - PySide2 复合小部件悬停效果
- javascript - 使用 Promise 替代 async / await