Recursively iterate through all subdirectories using pathlib
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
pathlib gives Python a cleaner, object-oriented way to work with paths, and it handles recursive traversal well once you know which method matches your goal. The most important choice is whether you want every path under a directory, only certain file types, or a custom traversal that can skip directories or handle errors explicitly.
The Main Recursive Tools in pathlib
The two methods most people reach for are rglob() and glob("**/*"). Both walk subdirectories recursively.
That prints every file and directory under project. If you only want Python files, change the pattern:
The equivalent glob form is:
In practice, rglob("*.py") is usually the easiest version to read.
Filtering Files and Directories
Recursive iteration often needs filtering after traversal. For example, you may want only regular files and not directories:
Or perhaps you want only subdirectories:
This pattern is clearer than trying to force complex logic into the glob expression itself.
When a Manual Recursive Walk Is Better
rglob() is convenient, but a manual recursive function gives you more control. This is useful when you want to skip hidden folders, avoid symbolic links, or recover from permission errors.
This version makes the traversal rules explicit. You can insert any condition you need before descending into a directory.
For example, skipping cache directories is straightforward:
That kind of logic is harder to express cleanly with rglob() alone.
Building Useful Results
A traversal loop often does more than print paths. You might collect file sizes, count extensions, or search for a name.
Because Path objects expose properties such as .name, .suffix, .stem, and .parent, post-processing stays readable.
If you only need the relative path from the root, use relative_to:
That is helpful when generating manifests or reports.
Error Handling and Symlinks
Recursive filesystem code can fail on unreadable directories or broken links. If you control the environment, rglob() may be enough. In mixed or user-controlled environments, manual recursion gives you room to handle exceptions.
Avoid following symlinks blindly in recursive code. A symlink can point upward in the directory tree and create repeated traversal or surprising results.
Choosing Between pathlib and os.walk
os.walk is still a good tool, especially if you need in-place control over the list of directories to descend into. pathlib is often preferred because the resulting code is more expressive and path operations are built in.
If you already use Path objects elsewhere in your application, staying inside pathlib avoids constant conversion between strings and paths.
Common Pitfalls
A common mistake is assuming iterdir() is recursive. It is not. It only yields immediate children of one directory level.
Another issue is forgetting to filter with is_file() or is_dir(). A recursive glob can return both, which leads to confusing downstream logic.
Symbolic links deserve special care. If you recurse through them without a rule, traversal can become unexpectedly large or cyclical.
Finally, do not assume recursive scanning is cheap. On large trees, it can be slow, so filter as early as possible and avoid unnecessary work inside the loop.
Summary
- Use
Path.rglob()for the shortest recursive traversal code. - Use a manual recursive generator when you need custom skip rules or error handling.
- Filter with
is_file()andis_dir()to make intent explicit. - Be careful with symbolic links and permission errors.
- Prefer
pathlibwhen you want readable path manipulation alongside traversal.
Related reading

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.