Download scripts/clean_pycache.py from a-k-dey/grip: direct link, hf CLI and curl.
- Browser
- Download file 1.23 kB
-
https://huggingface.co/spaces/a-k-dey/grip/resolve/main/scripts/clean_pycache.py
- Command line
-
hf download hf://spaces/a-k-dey/grip/scripts/clean_pycache.py
-
curl -L -o clean_pycache.py https://huggingface.co/spaces/a-k-dey/grip/resolve/main/scripts/clean_pycache.py
1.23 kB
| 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() | |