Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def remove_pycache(pyc=False):
- import os
- import re
- import shutil
- base_dir = input('Enter basedir (leave empty for current):\n')
- if not base_dir:
- base_dir = os.getcwd()
- if not os.path.exists(base_dir):
- print('Path does not exist.')
- return None
- if pyc:
- pattern = r'(?:.*)\.pyc$'
- counter = 0
- for (dirname, subdirs, fileshere) in os.walk(base_dir):
- pyc_files = list(filter(lambda x: re.match(pattern, x), fileshere))
- counter += len(pyc_files)
- list(os.remove(os.path.join(dirname, file)) for file in pyc_files)
- print(f"Removed {counter} .pyc files.")
- else:
- counter = 0
- for (dirname, subdirs, fileshere) in os.walk(base_dir):
- if '__pycache__' in subdirs:
- current_directory = os.path.join(dirname, '__pycache__')
- shutil.rmtree(current_directory)
- counter += 1
- print('Removing', current_directory)
- print(f'Removed {counter} __pycache__.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement