获取并删除当前目录下,包括内部子文件夹中的__pycache__
文件夹:
import os
import shutil
# 获取当前工作目录
root_dir = os.getcwd()
# 遍历目录
for dirpath, dirnames, filenames in os.walk(root_dir):
if "__pycache__" in dirnames:
# 获取 __pycache__ 目录的全路径
pycache_dir = os.path.join(dirpath, "__pycache__")
# 删除目录
shutil.rmtree(pycache_dir)
print(f"Removed: {pycache_dir}")