Last active
November 23, 2017 10:31
-
-
Save gaborantal/d98430e91ec7368a012d2b342c44d3e1 to your computer and use it in GitHub Desktop.
Remove Python cache files and folders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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