How to list only top level directories in Python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you want only the directories directly inside a given path, the task is simpler than a recursive walk. You just need to inspect the immediate entries of that directory and filter out anything that is not itself a directory.
Use pathlib for the Cleanest Code
pathlib is usually the most readable approach in modern Python.
This lists only the top-level directories inside /tmp. Files are ignored, and nested directories below those entries are not traversed.
If you want plain strings instead of Path objects, convert them with str(directory) or use directory.name for the final component only.
Use os.scandir for Efficient Filtering
os.scandir is also a strong choice, especially when performance matters.
os.scandir is efficient because each entry already carries file-type metadata, so checking whether something is a directory is cheaper than repeatedly joining paths and calling separate stat operations.
Avoid Full os.walk When You Only Need One Level
You may see os.walk used for this problem, but it is more than you need. os.walk is designed for recursive traversal.
If you do use it, the first yielded tuple contains the top-level subdirectory names:
This works, but pathlib.iterdir() or os.scandir() expresses the intent more directly.
Think About Hidden Directories and Symlinks
Depending on the use case, you may want to filter further. For example:
- skip hidden directories whose names start with a dot
- include or exclude symlinks intentionally
- sort the results for stable output
A filtered pathlib version might look like this:
That is often enough for scripts, build tools, and filesystem inspection tasks.
Error Handling
Filesystem code can fail because the path does not exist or the process lacks permission. Wrap the operation if the path is user-supplied or external.
For controlled internal paths, you may keep the code simpler, but for tools and utilities this error handling makes the behavior much more predictable.
Common Pitfalls
- Using
os.walkfor a one-level directory listing adds unnecessary recursion machinery. Preferpathlib.iterdir()oros.scandir()when you need only the top level. - Calling
os.listdir()and then checking each name with extra joined paths works, but it is less direct and often less efficient thanos.scandir(). - Forgetting to filter out files mixes unrelated entries into the result. Make the directory check explicit.
- Ignoring symlinks can produce surprising output if the path contains linked directories. Decide whether linked directories should count in your use case.
- Assuming the path always exists or is readable can make a simple utility crash unexpectedly. Handle
FileNotFoundErrorandPermissionErrorwhen appropriate.
Summary
- To list only top-level directories, inspect one directory level and filter for directory entries.
- '
pathlib.Path.iterdir()is clean and readable for modern Python.' - '
os.scandir()is also a strong option and is efficient.' - '
os.walk()can do it, but it is heavier than necessary for a non-recursive task.' - Decide how to handle hidden directories, symlinks, and filesystem errors based on your use case.

