clean_pkg_cache.py 787 B

1234567891011121314151617181920212223242526272829303132
  1. '''
  2. Function:
  3. Implementation of removepycache
  4. Author:
  5. Zhenchao Jin
  6. WeChat Official Account (微信公众号):
  7. Charles的皮卡丘
  8. '''
  9. from __future__ import annotations
  10. import os
  11. import shutil
  12. from pathlib import Path
  13. '''removepycache'''
  14. def removepycache(root: str | os.PathLike = ".") -> int:
  15. root_path, removed = Path(root).resolve(), 0
  16. for p in root_path.rglob("__pycache__"):
  17. if p.is_dir():
  18. try:
  19. shutil.rmtree(p)
  20. removed += 1
  21. print(f"Removed: {p}")
  22. except Exception as e:
  23. print(f"Failed: {p} ({e})")
  24. print(f"\nDone. Removed {removed} __pycache__ directories under {root_path}")
  25. return removed
  26. '''run'''
  27. if __name__ == "__main__":
  28. removepycache(".")