Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A Python walker that can ignore directories

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.

like image 201
Johan Carlsson Avatar asked May 29 '09 08:05

Johan Carlsson


People also ask

How do I walk a directory in Python?

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.

What is walk function in Python?

Python method walk() generates the file names in a directory tree by walking the tree either top-down or bottom-up.

What is the first element of the tuple returned by the OS walk method in Python?

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.


1 Answers

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
like image 174
Rick Copeland Avatar answered Nov 05 '22 20:11

Rick Copeland