Open links in new tab
  1. To list all folders within a given base path in Python, you can use the os or pathlib modules. These methods allow you to filter only directories from a given path efficiently.

    Example: Using os.listdir with os.path.isdir

    import os

    base_path = "/path/to/base"

    folders = [name for name in os.listdir(base_path)
    if os.path.isdir(os.path.join(base_path, name))]

    print(folders)
    Copied!

    This returns only the immediate subdirectories inside base_path.

    Using os.scandir (Faster for Large Directories) os.scandir() is more efficient than os.listdir() because it retrieves file attributes along with names, avoiding extra system calls.

    import os

    base_path = "/path/to/base"

    folders = [entry.name for entry in os.scandir(base_path) if entry.is_dir()]
    print(folders)
    Copied!

    This method is recommended when performance matters, especially for large directory trees.

    Using pathlib (Modern and Readable) The pathlib module provides an object-oriented approach to filesystem paths.

    from pathlib import Path

    base_path = Path("/path/to/base")

    folders = [f.name for f in base_path.iterdir() if f.is_dir()]
    print(folders)
    Copied!
  1. Getting a list of all subdirectories in the current directory

    Jun 10, 2009 · Is there a way to return a list of all the subdirectories in the current directory in Python? I know you can do this with files, but I need to get the list of directories instead.

    • Reviews: 2
      Usage example
      [x[0] for x in os.walk(directory)]
    • Python - List Files in a Directory - GeeksforGeeks

      Jul 23, 2025 · Sometimes, while working with files in Python, a problem arises with how to get all files in a directory. In this article, we will cover different methods of …

    • Python: Finding Files in a Directory - CodeRivers

      Apr 5, 2025 · Python provides several built-in libraries and functions that make it relatively easy to search for files in a directory. This blog post will explore these methods, from the basic to more …

    • How to Get a List of All Files in a Directory With Python

      Jan 12, 2025 · In this tutorial, you'll be examining a couple of methods to get a list of files and folders in a directory with Python. You'll also use both methods to …

    • Find a file in python - Stack Overflow

      Nov 12, 2009 · When running the program, even if you are looking for a single …

      • Reviews: 1
      • Find Files in Python: Complete Cookbook for Searching …

        However, finding files in Python can be daunting if you don’t know where to start, especially since you can choose between more than one library to find and …

      • Python Directory Management - GeeksforGeeks

        Sep 8, 2025 · Let’s explore how to retrieve the last access and modification times of files or directories in Python. This is useful for tracking file usage, backups or system monitoring tasks.

      • File and Directory Access — Python 3.14.3 documentation

        2 days ago · The modules described in this chapter deal with disk files and directories. For example, there are modules for reading the properties of files, …

      • Python: Using os.listdir to List Files in a Directory - PyTutorial

        Nov 15, 2024 · Learn how to use Python's os.listdir () method to list all files and directories in a specified path. Suitable for beginners with examples.

      • How to List All Files in Directory and Subdirectories in …

        Feb 2, 2024 · This tutorial demonstrates how to list all files in a directory and subdirectories in Python.

      • People also ask
        Loading
        Unable to load answer