Python list directory, subdirectory, and files
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Python gives you several good ways to list directories and files, and the best one depends on whether you want a flat listing or a recursive walk. For new code, pathlib is usually the clearest API, while os.walk remains a strong choice when you want explicit control over directory traversal.
The key is to decide early whether you need names, full paths, only files, only directories, or a complete recursive tree. Once that is clear, the implementation becomes simple.
Use pathlib for a Flat Listing
For modern Python code, pathlib.Path is often the most readable option.
iterdir() lists the immediate contents of one directory. It does not recurse into subdirectories.
You can also separate files and directories easily:
Use os.walk for Recursive Traversal
If you need to visit a directory tree recursively, os.walk is the classic tool.
This is useful because each iteration already gives you the current directory, the subdirectories under it, and the files in it. You do not need to call isfile or isdir manually for every entry just to understand the tree structure.
Recursive Listing with pathlib
If you like pathlib, you can still recurse with rglob.
This is concise and works well for many scripts. It is especially convenient when you also want pattern matching, such as only listing Python files.
Build Full Paths Correctly
A common beginner mistake is to call os.listdir and then forget that it returns only entry names, not full paths.
If you need full paths often, pathlib avoids this problem because each item is already a Path object that knows its own location.
Print a Simple Directory Tree
If the goal is to display the structure clearly, combine recursion with indentation.
This is useful for quick diagnostics, CLI tools, and file-audit scripts.
Choose the Right Tool
A practical rule is:
- use
Path.iterdir()for one directory - use
os.walk()for structured recursive traversal - use
Path.rglob()for recursive pattern-based searches
There is no need to force one API into every situation. The right answer depends on how much control and filtering you need.
Common Pitfalls
- Using
os.listdir()and forgetting that the results are names rather than full paths. - Recursing manually when
os.walk()orrglob()would already do the job. - Assuming hidden files are excluded by default when they usually are not.
- Mixing string paths and
Pathobjects inconsistently in the same function. - Walking huge directory trees without any filtering and then wondering why the script is slow.
Summary
- '
pathlib.Path.iterdir()is a clean choice for listing one directory.' - '
os.walk()is a strong built-in tool for recursive directory traversal.' - '
Path.rglob()is convenient when you want recursive searches with patterns.' - Be careful about full paths versus bare entry names.
- Pick the API based on whether you need flat listing, recursion, or pattern matching.
Related reading
- Python List of dict, if exists increment a dict value, if not append a new dict
- Python list sort in descending order
- Python list vs. array – when to use?
- Python List vs Dict for look up table
- Python locale error unsupported locale setting
- Python Logging - Disable logging from imported modules
- Python logging not outputting anything
- Python logging use milliseconds in time format
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.