Skip to content

Instantly share code, notes, and snippets.

@akx
Created February 18, 2018 16:47
Show Gist options
  • Select an option

  • Save akx/a5afb126b43cf2a35d785dc6dc1b039d to your computer and use it in GitHub Desktop.

Select an option

Save akx/a5afb126b43cf2a35d785dc6dc1b039d to your computer and use it in GitHub Desktop.
delete_libraries.py - delete node_moduleses and venvs
import os
import sys
import argparse
import shutil
import traceback
def process(path, interactive=False):
if not interactive:
print(path)
return
if input('Delete {}? [y/N] '.format(path)) == 'y':
def handle_error(func, pth, excinfo):
print('{}: error removing {}: {}'.format(
path,
pth,
excinfo,
))
shutil.rmtree(path, ignore_errors=False, onerror=handle_error)
def main():
ap = argparse.ArgumentParser()
ap.add_argument('roots', nargs='+', metavar='root')
ap.add_argument('-i', '--interactive', action='store_true')
args = ap.parse_args()
for root in args.roots:
for dirpath, dirnames, filenames in os.walk(root):
new_dirnames = []
for dirname in dirnames:
if dirname in ('node_modules', 'venv'):
nm_path = os.path.join(dirpath, dirname)
process(nm_path, interactive=args.interactive)
continue
new_dirnames.append(dirname)
dirnames[:] = new_dirnames
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment