How to Walk Directory Tree in Python Using os.walk
Use the
os.walk() function in Python to walk through a directory tree. It yields a tuple with the current directory path, a list of subdirectories, and a list of files, allowing you to process each folder and file step-by-step.Syntax
The os.walk() function is used to generate the file names in a directory tree by walking either top-down or bottom-up.
- os.walk(top, topdown=True, onerror=None, followlinks=False)
top: The root directory path to start walking from.topdown: IfTrue, directories are scanned from top to bottom; ifFalse, from bottom to top.onerror: Optional function called with an OSError instance if an error occurs.followlinks: IfTrue, symbolic links to directories are followed.
It returns a generator yielding tuples: (dirpath, dirnames, filenames).
python
import os for dirpath, dirnames, filenames in os.walk('path/to/start'): print(f'Current directory: {dirpath}') print(f'Subdirectories: {dirnames}') print(f'Files: {filenames}') print('---')
Example
This example walks through the current directory and prints all folders and files it finds. It shows how to access each part of the directory tree step-by-step.
python
import os start_path = '.' # current directory for dirpath, dirnames, filenames in os.walk(start_path): print(f'Found directory: {dirpath}') for dirname in dirnames: print(f' Subdirectory: {dirname}') for filename in filenames: print(f' File: {filename}')
Output
Found directory: .
Subdirectory: folder1
Subdirectory: folder2
File: file1.txt
File: file2.py
Found directory: ./folder1
File: file3.txt
Found directory: ./folder2
Subdirectory: subfolder
Found directory: ./folder2/subfolder
File: file4.txt
Common Pitfalls
- Not using the correct path for
os.walk()can cause no output or errors. - Modifying
dirnamesinside the loop affects which directories are walked. - Forgetting that
os.walk()returns a generator, so you must iterate it to get results. - Not handling symbolic links properly can cause infinite loops if
followlinks=True.
Example of filtering out hidden directories:
python
import os start_path = '.' for dirpath, dirnames, filenames in os.walk(start_path): # Remove hidden directories from traversal dirnames[:] = [d for d in dirnames if not d.startswith('.')] print(f'Walking: {dirpath}') print(f'Directories: {dirnames}') print(f'Files: {filenames}')
Quick Reference
- os.walk(path): Walk directory tree from
path. - Yields (
dirpath,dirnames,filenames). - Modify
dirnamesto control traversal. - Use
topdown=Trueto process parent folders before children. - Set
followlinks=Trueto follow symbolic links carefully.
Key Takeaways
Use os.walk() to recursively traverse directories and files easily.
os.walk() yields a tuple with current path, subdirectories, and files for each folder.
Modify the dirnames list inside the loop to skip or control which folders to visit.
Be careful with symbolic links to avoid infinite loops when following them.
Always provide the correct starting path to os.walk() for expected results.