How to Walk a Directory Tree with os.walk in Python

A generator function that recursively walks a directory tree and yields every file path found using the os.walk generator.

Easy Python 3.9+ Aug 9, 2026 Files & data 12 views 0 copies

Python code

26 lines
Python 3.9+
import os


def walk_directory_tree(root_path: str):
    """Walk a directory tree and yield file paths using os.walk generator."""
    for dirpath, dirnames, filenames in os.walk(root_path):
        for filename in filenames:
            yield os.path.join(dirpath, filename)


if __name__ == "__main__":
    # Create a small test directory structure
    test_dir = "test_tree"
    os.makedirs(os.path.join(test_dir, "subdir1"), exist_ok=True)
    os.makedirs(os.path.join(test_dir, "subdir2"), exist_ok=True)

    with open(os.path.join(test_dir, "file1.txt"), "w") as f:
        f.write("Hello")
    with open(os.path.join(test_dir, "subdir1", "file2.py"), "w") as f:
        f.write("print('hi')")
    with open(os.path.join(test_dir, "subdir2", "file3.md"), "w") as f:
        f.write("# Title")

    # Walk and display all files
    for file_path in walk_directory_tree(test_dir):
        print(file_path)

Output

stdout
test_tree/file1.txt
test_tree/subdir1/file2.py
test_tree/subdir2/file3.md

How it works

os.walk returns tuples of (dirpath, dirnames, filenames) for each directory it visits. The generator yields each file path using os.path.join to build a platform-independent full path. Files are yielded one at a time, so memory stays low even on very large trees. Because walk_directory_tree is a generator, you can iterate it lazily with a for loop.

Common mistakes

  • Confusing os.walk with os.listdir — os.walk recurses automatically
  • Modifying the `dirnames` list during iteration, which changes traversal order
  • Forgetting `os.path.join` and building paths manually with strings — breaks on Windows

Variations

  1. Use `pathlib.Path.rglob('*')` to walk and filter files in one pass
  2. Add `topdown=False` to os.walk to process files before their parent directory

Real-world use cases

  • Scanning a media library to build a searchable index of file paths.
  • Batch-renaming or moving thousands of files across nested project folders.
  • Collecting all log files across a server's directory structure for an audit script.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Files & data

Related tutorials and quizzes for this topic.