If you're using Python 3, you can use Path
from pathlib
module with rglob
function to find only node_modules
directory. That way you will only iterate through node_modules
directory in your for
loop and excluding other files
import os
import time
import shutil
from pathlib import Path
PATH = "/Users/wagnermattei/www"
now = time.time()
old = now - 1296000
for path in Path(PATH).rglob('node_modules'):
abs_path = str(path.absolute())
if os.path.getmtime(abs_path) < old:
print('Deleting: ' + abs_path)
shutil.rmtree(abs_path)
Update: If you don't want to check node_modules
directory if one of its parent directories is also a node_modules
and is deleted. You can use os.listdir
instead to non-recursively list all the directories in the current directory and use it with a recursive function so that you can traverse down the directory tree and will always check the parent directories first before checking their subdirectories. If the parent directory is an unused node_modules
, you can delete that directory and don't traverse further down to the subdirectories
import os
import time
import shutil
PATH = "/Users/wagnermattei/www"
now = time.time()
old = now - 1296000
def traverse(path):
dirs = os.listdir(path)
for d in dirs:
abs_path = os.path.join(path, d)
if d == 'node_modules' and os.path.getmtime(abs_path) < old:
print('Deleting: ' + abs_path)
shutil.rmtree(abs_path)
else:
traverse(abs_path)
traverse(PATH)