The os module of Python provides a function called walk that makes it easy to go over all the directories and files in a directory tree.

It is also quite easy to skip some of the subtrees.

Here you can see an example how to go over a directory tree.

Changing the content of dir variable allows us to skip some of the subdirectories.

In the first example we only skip the .git directory, in the next example that is currently committed out, we have a more complex filter that will make it skip a number of different subdirectories.


import os
import sys

if len(sys.argv) != 2:
    exit("Usage: {} PATH_TO_DIRECTORY".format(sys.argv[0]))

root = sys.argv[1]

for dirname, dirs, files in os.walk(root):
    #print(dirname)     # relative path (from cwd) to the directory being processed
    #print(dirs)       # list of subdirectories in the currently processed directory
    #print(files)       # list of files in the currently processed directory
    for filename in files:
        print(os.path.join(dirname, filename))   # relative path to the "current" file

    dirs[:] = [d for d in dirs if d != '.git']
    #dirs[:] = [d for d in dirs if not d.startswith('.') and not d.startswith('__') and d not in ['tmp', 'logs']]