wmattei
wmattei

Reputation: 494

How can i improve this function to delete old node_modules folders

The goal of this script is to delete all node_modules that haven't been touch in the last 15 days.

It is currently working, but as it goes inside each folder because of os.walk i lose efficiency as i don't have to go inside a node_modules folder because it's exactly what i want to delete

import os
import time
import shutil

PATH = "/Users/wagnermattei/www"

now = time.time()
old = now - 1296000
for root, dirs, files in os.walk(PATH, topdown=False):
    for _dir in dirs:
        if _dir == 'node_modules' and os.path.getmtime(os.path.join(root, _dir)) < old:
            print('Deleting: '+os.path.join(root, _dir))
            shutil.rmtree(os.path.join(root, _dir))

Upvotes: 2

Views: 99

Answers (2)

VietHTran
VietHTran

Reputation: 2318

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)

Upvotes: 2

NadTraps
NadTraps

Reputation: 76

List comprehensions are more effective in python that for loops. But I'm not sure if it's better for debugging.

You should try this:

[shutil.rmtree(os.path.join(root, _dir) \
for root, dirs, files in os.walk(PATH, topdown=False) \
    for _dir in dirs \
        if _dir == 'node_modules' and os.path.getmtime(os.path.join(root, _dir)) < old ]

But I think you should use npm to manage the old packages. Maybe this post can help :)

Upvotes: 1

Related Questions