Skip to content

Instantly share code, notes, and snippets.

@gaborantal
Last active November 23, 2017 10:31
Show Gist options
  • Save gaborantal/d98430e91ec7368a012d2b342c44d3e1 to your computer and use it in GitHub Desktop.
Save gaborantal/d98430e91ec7368a012d2b342c44d3e1 to your computer and use it in GitHub Desktop.
Remove Python cache files and folders
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import shutil
def remove_python_cache(directory, verbose=False):
for root, dir_names, file_names in os.walk(directory, topdown=False):
for dir_name in dir_names:
if dir_name == '__pycache__':
shutil.rmtree(os.path.join(root, dir_name))
if verbose:
print('%s removed!' % os.path.join(root, dir_name))
for file_name in file_names:
if file_name.split('.')[-1] in ['pyc', 'pyo']:
os.remove(os.path.join(root, file_name))
if verbose:
print('%s removed!' % os.path.join(root, file_name))
if __name__ == '__main__':
remove_python_cache(r'c:\path\to\project')
# or
# remove_python_cache(r'/home/path/to/project')
print('Removed Python cache')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment