Files
primitive-type/clean_pycache.py

38 lines
1.0 KiB
Python

#!/usr/bin/env python3
import os
import shutil
import sys
def remove_pycache_dirs(start_dir="."):
removed_count = 0
for root, dirs, files in os.walk(start_dir, topdown=True):
if ".venv" in dirs:
dirs.remove(".venv")
if ".git" in dirs:
dirs.remove(".git")
if "__pycache__" in dirs:
pycache_path = os.path.join(root, "__pycache__")
try:
shutil.rmtree(pycache_path)
print(f"Deleted: {pycache_path}")
removed_count += 1
except Exception as e:
print(f"Failed to delete {pycache_path}: {e}")
print(f"\nTotally deleted {removed_count} __pycache__ folders.")
if __name__ == "__main__":
target_dir = sys.argv[1] if len(sys.argv) > 1 else "."
print(f"Scanning directory: {os.path.abspath(target_dir)}")
confirm = input("Are you sure to delete all __pycache__ folders? (y/N): ")
if confirm.lower() == "y":
remove_pycache_dirs(target_dir)
else:
print("Option cancelled.")