File size: 1,232 Bytes
d50f06d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import os
import shutil
def clean_pycache(directory="."):
"""
Recursively remove all __pycache__ directories and .pyc files
"""
cleaned_count = 0
print(f"Starting cleanup in: {os.path.abspath(directory)}")
for root, dirs, files in os.walk(directory):
# Remove __pycache__ directories
if "__pycache__" in dirs:
pycache_path = os.path.join(root, "__pycache__")
try:
shutil.rmtree(pycache_path)
print(f" Removed directory: {pycache_path}")
cleaned_count += 1
except Exception as e:
print(f" Error removing {pycache_path}: {e}")
# Remove orphaned .pyc and .pyo files
for file in files:
if file.endswith((".pyc", ".pyo")):
file_path = os.path.join(root, file)
try:
os.remove(file_path)
print(f" Removed file: {file_path}")
cleaned_count += 1
except Exception as e:
print(f" Error removing {file_path}: {e}")
print(f"\nCleanup finished. {cleaned_count} items removed.")
if __name__ == "__main__":
clean_pycache()
|