Python recursive folder read
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
Recursive folder reading means walking a directory tree and visiting files inside nested subdirectories. In modern Python, you usually do not need to write recursion yourself because the standard library already provides good traversal tools such as pathlib.Path.rglob and os.walk.
Use Path.rglob for simple recursive file discovery
If your real task is "find every file matching this pattern below a root directory", pathlib is usually the cleanest choice.
This yields Path objects, which makes later work such as reading text, checking suffixes, or accessing metadata straightforward.
A more practical example:
For many scripts, that is all you need.
Use os.walk when you need more control
os.walk is a better fit when the traversal itself matters. It gives you the current directory, the subdirectory list, and the filenames for each step of the walk.
Its biggest advantage is that you can prune the recursion by editing dirnames in place:
That is the key feature to remember. Modifying dirnames tells os.walk which subdirectories it should not descend into.
Open files carefully during traversal
Walking the directory tree is only part of the job. You often also need to read or parse each file. The safe pattern is to keep error handling close to the file operation so one unreadable file does not stop the whole scan.
This is a good pattern for search tools, simple indexers, and migration scripts.
It is also a good default for large trees because you process one file at a time instead of building a giant in-memory list of file contents before doing any real work.
Manual recursion is still possible
You can still write the walk recursively yourself, but that is usually only worth it when the traversal logic is custom enough that rglob and os.walk do not fit.
This works, but it is more code and easier to get wrong than the standard traversal helpers.
Common Pitfalls
- Reading every file eagerly into memory instead of processing one file at a time.
- Forgetting to skip irrelevant directories such as
.git,node_modules, or virtual environments. - Following symbolic links carelessly and ending up with repeated or confusing traversal paths.
- Assuming every discovered file is UTF-8 text and then failing on binary or differently encoded files.
- Writing manual recursion when
rgloboros.walkalready solves the problem more clearly.
Summary
- Use
Path.rglobwhen you mainly need recursive file matching. - Use
os.walkwhen you need directory-level control and pruning. - Handle file read errors near the file operation so one bad file does not stop the walk.
- Manual recursion is possible, but it is rarely the best default.
- Good traversal code is as much about exclusions and error handling as it is about recursion.
Related reading
- Python string 'in' operator implementation algorithm and time complexity
- Python weighted median algorithm with pandas
- Pythonic way to check if a list is sorted or not
- Pythonic way to check if a list is sorted or not
- Python, remove all non-alphabet chars from string
- python .replace regex
- Q-learning vs dynamic programming
- Quadrilateral Shape Finding Algorithm

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.