Skip to content

Instantly share code, notes, and snippets.

@mmngreco
Last active August 5, 2021 07:43
Show Gist options
  • Save mmngreco/afafe8c37498f8185d25b2648c57f73a to your computer and use it in GitHub Desktop.
Save mmngreco/afafe8c37498f8185d25b2648c57f73a to your computer and use it in GitHub Desktop.
[Pip clean] Python script to clean temp files created by pip. #tools #pip
import os, sys
from glob import glob
from pathlib import Path
def rmdir(pathlib_dir):
walked = []
if not pathlib_dir.exists():
return False
for item in pathlib_dir.iterdir():
walked.append(item)
if item.is_dir():
rmdir(item)
item.rmdir()
print(item)
else:
item.unlink()
print(item)
return walked
print("Welcome to pip cache cleaner")
print("============================")
OK = False
PLATFORM = sys.platform
if PLATFORM == "linux":
HOME = Path(os.environ["HOME"])
print("Cleaning pip cache. Platform %s" % sys.platform)
PATH_TO_RM = HOME / ".cache" / "pip"
rmdir(PATH_TO_RM)
OK = True
elif PLATFORM == "darwin":
HOME = Path(os.environ["HOME"])
print("Cleaning pip cache. Platform %s" % sys.platform)
PATH_TO_RM = HOME / "Library" / "Caches" / "pip"
rmdir(PATH_TO_RM)
OK = True
elif sys.platform == "win32":
print("Cleaning pip cache. Platform %s" % sys.platform)
LOCAL_APP_DATA = Path(os.environ["LOCALAPPDATA"])
rmdir(LOCAL_APP_DATA / "pip" / "cache")
[rmdir(Path(path)) for path in glob(str(LOCAL_APP_DATA / "Temp" / "pip-*"))]
OK = True
if OK:
print("pip cache cleaned!")
else:
print("pip cache not cleaned.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment