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.
Python code
26 linesimport 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
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
- Use `pathlib.Path.rglob('*')` to walk and filter files in one pass
- 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
More from Files & data
- Append a Line to a Log File in Python easy
- Audit File Permissions Across a Project in Python easy
- Automatically Detect Corrupted Files Using SHA-256 Checksums in Python easy
- Automatically Highlight Data Validation Errors Inside Excel Files in Python easy
- Build a Command-Line To-Do List Application with Data Persistence in Python easy
- Build a File Index by Relative Path Hash Map in Python easy
Keep learning
Related tutorials and quizzes for this topic.