chore: add a simple script to clean __pycache__ quickly

This commit is contained in:
2026-02-10 09:58:06 +08:00
parent 0c6b92ceeb
commit c5be89305a

37
clean_pycache.py Normal file
View File

@@ -0,0 +1,37 @@
#!/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.")