python - Recursively delete folders below 3rd level in large structure of folders -


i have large structure of thousands of folders only, interested in keeping folders in top 3 levels, , deleting rest. looking recursive python script this. appreciated.

untested, os.walk():

import os import shutil base = '.' root, dirs, files in os.walk(base): n = 0 head = root while head , head != base: head, _ = os.path.split(head) n += 1 if n == 3: dir in dirs: shutil.rmtree(os.path.join(root, dir)) del dirs[:] # clear dirs os.walk() doesn't subdirectories 

Comments

Popular posts from this blog

JQuery Autocomplete without using label, value, id -

c++ - Accessing inactive union member and undefined behavior? -

JAVA - what is the difference between void and boolean methods? -