How to List All Files in a Directory Using Python

Updated on May 6, 2026
Table of Contents
    The Solution

    To list all files in a directory using Python, you can use various methods from the os, glob, and pathlib modules to efficiently retrieve file names based on your specific needs.

    The Concept

    Listing files in a directory is a common task in programming, especially when dealing with file management and data processing. Python offers several built-in modules like os, glob, and pathlib that provide different methods to list files in a directory. Each method has its own advantages, and the choice depends on your specific requirements, such as whether you need to list files recursively or filter files by extension.

    Deep Technical Dive & Misconceptions

    The os module provides basic file and directory handling capabilities. The os.listdir() function lists all entries in a directory, including files and subdirectories. To filter only files, you can use os.path.isfile() in conjunction with os.listdir(). The os.walk() function allows recursive directory traversal, yielding a tuple of directory path, subdirectories, and files.

    The glob module is useful for pattern matching. It can list files using wildcard patterns, which is helpful for filtering files by extension or name pattern. The glob.glob() function returns a list of paths matching a pattern, while glob.iglob() returns an iterator, which can be more memory efficient for large directories.

    The pathlib module, introduced in Python 3.4, provides an object-oriented approach to handling file paths. With pathlib.Path().iterdir(), you can iterate over directory contents, and methods like glob() and rglob() allow for pattern-based and recursive file listing.

    Code Examples

    import os
    
    # List all files in the current directory
    files_in_directory = [f for f in os.listdir('.') if os.path.isfile(f)]
    print("Files:", files_in_directory)
    import os
    
    # Use os.walk to list all files in a directory and its subdirectories
    for root, dirs, files in os.walk('.'):  
        for file in files:  
            print(os.path.join(root, file))
    import glob
    
    # List all .txt files in the current directory
    text_files = glob.glob("*.txt")
    print("Text files:", text_files)
    from pathlib import Path
    
    # List all files in the current directory using pathlib
    current_directory = Path('.')
    all_files = [item for item in current_directory.iterdir() if item.is_file()]
    print("Files:", all_files)
    from pathlib import Path
    
    # Recursively list all .py files in the directory and subdirectories
    py_files = list(Path('.').rglob("*.py"))
    print("Python files:", py_files)

    Comparison Table

    Method Description Recursive Pattern Matching
    os.listdir() Lists all entries in a directory. No No
    os.walk() Generates file names in a directory tree. Yes No
    glob.glob() Lists files matching a pattern. Optional Yes
    pathlib.Path().iterdir() Iterates over directory contents. No No
    pathlib.Path().rglob() Recursively lists files matching a pattern. Yes Yes

    Frequently Asked Questions

    What is the difference between os.listdir() and os.walk()?

    os.listdir() lists all entries in a directory, while os.walk() traverses directories recursively, yielding a tuple of directory path, subdirectories, and files.

    How can I list only files with a specific extension?

    You can use the glob module with a pattern like *.txt to list files with a specific extension.

    Is pathlib more efficient than os for listing files?

    pathlib provides a more intuitive and object-oriented approach, but efficiency depends on the specific task. For complex path manipulations, pathlib is often more convenient.

    Can I list hidden files using glob?

    By default, glob does not list hidden files. You need to explicitly include patterns like .* to match hidden files.

    What is the advantage of using pathlib over os for file handling?

    pathlib offers a more readable and concise syntax for path manipulations, and it automatically handles different operating systems' path conventions.

    Was this solution helpful?
    Report Broken Code or Error

    Help us improve. Paste the error you got or explain what went wrong.

    Thanks! We will review and fix this.

    Related Questions

    Comments & Discussions

    Log in or Sign up to join the discussion.