I need a file system walker that I could instruct to ignore traversing directories that I want to leave untouched, including all subdirectories below that branch. The os.walk and os.path.walk just don't do it.
To traverse a directory in Python, use the os. walk() function. Each directory in the tree rooted at the top of the directory generates a 3-tuple: dirpath, dirnames, and filenames.
Python method walk() generates the file names in a directory tree by walking the tree either top-down or bottom-up.
The variable address on each iteration is associated with the first item of the current tuple (the string containing the address of the directory), dirs - with the second item (the list of subdirectories), and files - with a list of files in this directory.
Actually, os.walk
may do exactly what you want. Say I have a list (perhaps a set) of directories to ignore in ignore
. Then this should work:
def my_walk(top_dir, ignore):
for dirpath, dirnames, filenames in os.walk(top_dir):
dirnames[:] = [
dn for dn in dirnames
if os.path.join(dirpath, dn) not in ignore ]
yield dirpath, dirnames, filenames
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With