Skip to content

Instantly share code, notes, and snippets.

@ozankaraali
Created January 14, 2024 13:36
Show Gist options
  • Save ozankaraali/b41612f4c6c6f3124a1f36c6e8e1235d to your computer and use it in GitHub Desktop.
Save ozankaraali/b41612f4c6c6f3124a1f36c6e8e1235d to your computer and use it in GitHub Desktop.
This script removes all the folders named venv, and node_modules.
import os
import shutil
def remove_folders_recursively(start_path, folder_names, exclude_path):
for root, dirs, files in os.walk(start_path, topdown=True):
# Skip the excluded directory
dirs[:] = [d for d in dirs if os.path.join(root, d) != exclude_path]
for dir_name in dirs:
if dir_name in folder_names:
full_path = os.path.join(root, dir_name)
print(f"Removing {full_path}")
shutil.rmtree(full_path, ignore_errors=True)
home_directory = os.path.expanduser("~") # Gets your home directory
folders_to_remove = ['venv', 'node_modules', '.venv']
exclude_directory = os.path.join(home_directory, 'Library') # Path to the directory to exclude
remove_folders_recursively(home_directory, folders_to_remove, exclude_directory)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment