Python

How to list only top level directories in Python

27 September 2026 · 9 min read

How to list only top level directories in Python

Navigating file systems is a common task in Python programming, and sometimes you only need to list only top level directories in Python. This can be more efficient and cleaner than traversing entire directory trees, especially when dealing with large file systems. Whether you’re automating backups, managing project structures, or building file explorers, understanding how to specifically target top-level directories is crucial. This article provides a comprehensive guide on how to achieve this using Python’s built-in modules and best practices. We’ll explore different methods and delve into practical examples to ensure you can confidently implement these techniques in your projects, helping you streamline your file management tasks and improve your overall Python coding skills. Effective directory management is essential for any programmer working with files.

Understanding the Basics: Python’s os and os.path Modules

Python provides robust tools for interacting with the operating system through the os and os.path modules. These modules are essential for tasks like listing files, creating directories, and manipulating paths. When you need to list only top level directories in Python, these modules offer the necessary functions. The os.listdir() function, for example, returns a list of all files and directories in a specified path. However, it doesn’t differentiate between files and directories by default. That’s where os.path.isdir() comes in handy. It allows you to check whether a given path is a directory, enabling you to filter the results from os.listdir() and extract only the directories.

Using these modules effectively requires understanding how they interact. os.listdir() gives you the names of items within a directory, but to confirm if each item is a directory, you need to use os.path.join() to create the full path and then pass that path to os.path.isdir(). This combination of functions allows for precise control over what you identify and process within your file system. Consider this as the foundation for more complex file management scripts. A proper understanding of these tools helps in managing files and improving code efficiency.

Furthermore, the os.scandir() function, introduced in Python 3.5, is often more efficient than os.listdir() when you need both the name and type information (file or directory). os.scandir() returns an iterator of DirEntry objects, each containing attributes like name and methods like is_dir(), which can simplify your code and improve performance. According to the Python documentation, os.scandir() can significantly reduce the overhead of making multiple system calls to determine file types, especially in large directories. Official Python Documentation supports this with performance comparisons.

Methods to List Only Top Level Directories

There are several ways to list only top level directories in Python, each with its own advantages. The most common approach involves using a combination of os.listdir() or os.scandir() and os.path.isdir(). Another method involves list comprehensions, providing a concise way to filter directories. Choosing the right method depends on factors like code readability, performance requirements, and the specific Python version you’re using. Understanding these different techniques allows you to optimize your code for various scenarios.

One straightforward method is to iterate through the items returned by os.listdir() and use os.path.isdir() to check if each item is a directory. If it is, you add it to a list. This approach is easy to understand and implement, making it suitable for beginners. The code is readable and clearly expresses the intention. However, it might not be the most efficient for large directories due to the overhead of multiple function calls. Here’s an example:

import os def get_top_level_directories_listdir(path): directories = [] for item in os.listdir(path): item_path = os.path.join(path, item) if os.path.isdir(item_path): directories.append(item) return directories 

Alternatively, os.scandir() offers a more efficient solution. Instead of making separate calls to os.path.join() and os.path.isdir(), you can directly use the is_dir() method of the DirEntry object. This reduces the number of system calls and improves performance, especially when dealing with a large number of files and directories. Below is an example using os.scandir():

import os def get_top_level_directories_scandir(path): directories = [] for entry in os.scandir(path): if entry.is_dir(): directories.append(entry.name) return directories 

The following paragraph is optimized for a featured snippet:

To efficiently list only top level directories in Python, use the os.scandir() function in combination with the is_dir() method. This approach iterates through directory entries, directly checking if each entry is a directory without the need for separate path joining and type checking. This method reduces system calls, making it faster and more efficient than using os.listdir() and os.path.isdir() separately. The result is a cleaner and more performant solution for extracting directory names from a given path.

Practical Examples and Use Cases

Knowing how to list only top level directories in Python is useful in many real-world scenarios. Consider a project that involves processing data from multiple directories, such as log files from different servers or user data organized by date. Being able to quickly identify and iterate through these top-level directories can streamline your data processing pipeline. Another use case is creating a custom file explorer or directory management tool, where you need to display only the main directories to the user. These examples show how the ability to list directories is essential for various tasks.

For example, let’s say you are building a backup script that needs to archive each top-level directory separately. You could use the techniques described above to identify the directories and then use a library like shutil to create zip files for each one. This automates the backup process and ensures that each directory is backed up individually. The following code demonstrates how to achieve this:

import os import shutil def backup_top_level_directories(source_path, destination_path): directories = get_top_level_directories_scandir(source_path) for directory in directories: source_dir = os.path.join(source_path, directory) destination_zip = os.path.join(destination_path, directory + '.zip') shutil.make_archive(destination_zip[:-4], 'zip', source_dir) Removing '.zip' extension for shutil 

Another practical example involves managing a large collection of images organized into directories based on categories (e.g., ‘animals’, ’landscapes’, ‘portraits’). If you want to generate a report summarizing the number of images in each category, you can use the techniques discussed to list the category directories and then count the number of image files within each. This provides a quick overview of your image collection. The ability to efficiently navigate file systems is crucial for any programmer working with files and directories.

Best Practices and Optimization Tips

When working to list only top level directories in Python, following best practices can significantly improve the efficiency and maintainability of your code. One important tip is to handle exceptions gracefully. For example, if you’re dealing with network drives or external storage, there’s a chance that a directory might be inaccessible. Wrapping your code in try…except blocks can prevent your script from crashing and provide informative error messages.

Another optimization tip is to use absolute paths instead of relative paths whenever possible. Absolute paths provide a clear and unambiguous reference to a file or directory, reducing the risk of errors caused by changes in the current working directory. Additionally, consider using caching techniques if you need to repeatedly access the same directory structure. Caching the results of os.listdir() or os.scandir() can avoid redundant system calls and improve performance. Real Python’s Pathlib Tutorial provides useful insights into efficient path management.

Here are some key points to remember:

  • Use os.scandir() for better performance, especially in Python 3.5 and later.
  • Handle exceptions to prevent crashes when dealing with inaccessible directories.
  • Use absolute paths for clarity and to avoid potential errors.

Here are steps for listing top-level directories:

  1. Import the os module.
  2. Define a function that takes a path as input.
  3. Use os.scandir() to iterate through the directory.
  4. Check if each entry is a directory using entry.is_dir().
  5. Append the directory name to a list.
  6. Return the list of directories.

Here are some potential issues to keep in mind:

  • Permissions issues when accessing certain directories.
  • Symbolic links that might lead to unexpected behavior.
  • Large directories that can take a long time to scan.

By following these best practices, you can write more robust and efficient code for listing top-level directories in Python. Proper error handling, efficient path management, and careful consideration of potential issues are essential for creating reliable file management scripts.

FAQ Section

What is the difference between os.listdir() and os.scandir()?
os.listdir() returns a list of filenames in a directory, while os.scandir() returns an iterator of DirEntry objects. os.scandir() is generally more efficient when you need information about the files (e.g., type, size) because it avoids making separate system calls for each file.
How do I handle permission errors when listing directories?
Wrap your code in a try...except block and catch the PermissionError exception. This allows you to gracefully handle cases where you don't have permission to access a directory.
Can I use pathlib to list top-level directories?
Yes, the pathlib module provides an object-oriented way to interact with the file system. You can use Path.iterdir() to iterate through the directory and Path.is\_dir() to check if each entry is a directory. [Python's Pathlib Documentation](https://docs.python.org/3/library/pathlib.html) offers more detailed information.
Infographic here: Comparison of os.listdir() and os.scandir() performance
[Learn More](https://courthousezoological.com/n7sqp6kh?key=e6dd02bc5dbf461b97a9da08df84d31c)Mastering the art of **list only top level directories in Python** is a valuable skill for any Python developer. By understanding the nuances of the os and os.path modules, and by employing best practices for error handling and optimization, you can write efficient and robust code for managing file systems. The examples provided in this article should give you a solid foundation for tackling a variety of file management tasks.

Now that you’ve explored these methods, consider how you can apply them to your own projects. Experiment with different approaches, measure their performance, and adapt them to your specific needs. Whether you’re automating backups, building file explorers, or managing data pipelines, the ability to efficiently list directories is a powerful tool. Why not start by refactoring an existing project to use os.scandir() and see the performance benefits firsthand? Dive in, experiment, and continue to refine your skills in Python file management. Consider exploring related topics like “Python file system monitoring” or “Automating directory backups with Python” to further enhance your expertise.

Question & Answer :
I want to be able to list only the directories inside some folder. This means I don’t want filenames listed, nor do I want additional sub-folders.

Let’s see if an example helps. In the current directory we have:

>>> os.listdir(os.getcwd()) ['cx_Oracle-doc', 'DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'mod_p ython-wininst.log', 'NEWS.txt', 'pymssql-wininst.log', 'python.exe', 'pythonw.ex e', 'README.txt', 'Removemod_python.exe', 'Removepymssql.exe', 'Scripts', 'tcl', 'Tools', 'w9xpopen.exe'] 

However, I don’t want filenames listed. Nor do I want sub-folders such as \Lib\curses. Essentially what I want works with the following:

>>> for root, dirnames, filenames in os.walk('.'): ... print dirnames ... break ... ['cx_Oracle-doc', 'DLLs', 'Doc', 'include', 'Lib', 'libs', 'Scripts', 'tcl', 'Tools'] 

However, I’m wondering if there’s a simpler way of achieving the same results. I get the impression that using os.walk only to return the top level is inefficient/too much.

os.walk

Use os.walk with next item function:

next(os.walk('.'))[1] 

For Python <=2.5 use:

os.walk('.').next()[1] 

How this works

os.walk is a generator and calling next will get the first result in the form of a 3-tuple (dirpath, dirnames, filenames). Thus the [1] index returns only the dirnames from that tuple.