You can modify dirs in place to prevent further traversal into the directory structure.
for root, dirs, files in os.walk('site_dump'):
nested_levels = root.split('/')
if len(nested_levels) == 5:
del dirs[:]
# Eventually do stuff with the file here
del dirs[:]
will remove the contents of the list, rather than replace dirs with a reference to a new list. When doing this it is important to modify the list in-place.
From the docs, with topdown
referring to an optional parameter for os.walk
that you omitted and defaults to True
:
When topdown is True, the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, impose a specific order of visiting, or even to inform walk() about directories the caller creates or renames before it resumes walk() again. Modifying dirnames when topdown is False is ineffective, because in bottom-up mode the directories in dirnames are generated before dirpath itself is generated.