Created
February 2, 2024 08:25
-
-
Save Maxiviper117/1825f619890e0d7953a58a81920049d2 to your computer and use it in GitHub Desktop.
Delete all node_modules in a specified directory using Python
This file contains hidden or 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
from pathlib import Path | |
import shutil | |
def delete_node_modules(directory): | |
deleted_any = False # Track if any node_modules directories have been deleted | |
for path in directory.rglob('*'): # Use rglob to find all paths | |
if path.name == 'node_modules' and path.is_dir(): | |
shutil.rmtree(path) | |
print(f"Deleted {path}") | |
deleted_any = True | |
return deleted_any | |
# Example usage | |
directory_to_search = Path('/path/to/your/directory') | |
if not delete_node_modules(directory_to_search): | |
print("No node_modules directory found.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment