}

Python: how to recursively search for files traversing directories

Created:

Introduction

In this tutorial we are going to explain how to recursively browse a directory to process all files traversing all directories. We are going to give code that works for Python 2 and 3 using standard lib.

Using os.walk

Python os.walk is a generator that navigates the directory tree top-down or buttom-up and yields directory path, directory names and files.

We created a function search_files with two parameters directory with accepts a string with the path to search and extension with allows to filter files by extension.

import os

def search_files(directory='.', extension=''):
    extension = extension.lower()
    for dirpath, dirnames, files in os.walk(directory):
        for name in files:
            if extension and name.lower().endswith(extension):
                print(os.path.join(dirpath, name))
            elif not extension:
                print(os.path.join(dirpath, name))

We change the filename to lower to avoid confusion while using extension argument case, but you can remove lower() to be case sensitive. Replace the print with you function or custom code to process the desired files.