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

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -