Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. def remove_pycache(pyc=False):
  2.     import os
  3.     import re
  4.     import shutil
  5.  
  6.     base_dir = input('Enter basedir (leave empty for current):\n')
  7.  
  8.     if not base_dir:
  9.         base_dir = os.getcwd()
  10.  
  11.     if not os.path.exists(base_dir):
  12.         print('Path does not exist.')
  13.         return None
  14.  
  15.     if pyc:
  16.         pattern = r'(?:.*)\.pyc$'
  17.         counter = 0
  18.         for (dirname, subdirs, fileshere) in os.walk(base_dir):
  19.             pyc_files = list(filter(lambda x: re.match(pattern, x), fileshere))
  20.             counter += len(pyc_files)
  21.             list(os.remove(os.path.join(dirname, file)) for file in pyc_files)
  22.         print(f"Removed {counter} .pyc files.")
  23.     else:
  24.         counter = 0
  25.         for (dirname, subdirs, fileshere) in os.walk(base_dir):
  26.             if '__pycache__' in subdirs:
  27.                 current_directory = os.path.join(dirname, '__pycache__')
  28.                 shutil.rmtree(current_directory)
  29.                 counter += 1
  30.                 print('Removing', current_directory)
  31.         print(f'Removed {counter} __pycache__.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement