How do I list all files of a directory?

Learn how do i list all files of a directory? with practical examples, diagrams, and best practices. Covers python, directory development techniques with visual explanations.

How to List All Files in a Directory Using Python

Hero image for How do I list all files of a directory?

Learn various Python methods to efficiently list files and directories, including recursive and filtered listings, with practical code examples.

Listing the contents of a directory is a fundamental operation in many programming tasks, from file management utilities to data processing scripts. Python offers several robust and flexible ways to achieve this, catering to different needs such as listing only files, including subdirectories, or filtering by specific criteria. This article will guide you through the most common and effective methods available in Python.

Understanding Python's OS Module for Directory Operations

The os module in Python provides a portable way of using operating system dependent functionality. When it comes to directory and file operations, it's your primary tool. Two key functions within this module are os.listdir() and os.walk(). While os.listdir() gives you a flat list of entries in a single directory, os.walk() is designed for traversing entire directory trees.

flowchart TD
    A[Start] --> B{Choose Method}
    B -->|Single Directory| C[os.listdir()]
    B -->|Directory Tree| D[os.walk()]
    C --> E{Filter?}
    D --> F{Filter?}
    E --> G[List Comprehension/Loop]
    F --> H[List Comprehension/Loop]
    G --> I[Process Files]
    H --> I
    I --> J[End]

Decision flow for listing directory contents in Python.

Listing Files in a Single Directory with os.listdir()

The os.listdir(path) function returns a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It includes both files and subdirectories. To get only files, you'll need to combine it with os.path.isfile().

import os

def list_files_in_directory(directory_path):
    """Lists all files in a specified directory (non-recursive)."""
    files = []
    try:
        for entry in os.listdir(directory_path):
            full_path = os.path.join(directory_path, entry)
            if os.path.isfile(full_path):
                files.append(entry)
    except FileNotFoundError:
        print(f"Error: Directory '{directory_path}' not found.")
    except PermissionError:
        print(f"Error: Permission denied to access '{directory_path}'.")
    return files

# Example usage:
current_directory = '.' # Represents the current working directory
all_files = list_files_in_directory(current_directory)
print(f"Files in '{current_directory}': {all_files}")

# Example with a specific path
# my_path = '/path/to/your/directory'
# my_files = list_files_in_directory(my_path)
# print(f"Files in '{my_path}': {my_files}")

Python code to list only files in a given directory using os.listdir().

Recursively Listing Files with os.walk()

For scenarios where you need to list files not just in a single directory but also in all its subdirectories, os.walk() is the ideal tool. It generates the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at top (including top itself), it yields a 3-tuple: (dirpath, dirnames, filenames).

import os

def list_all_files_recursive(root_dir):
    """Recursively lists all files in a directory and its subdirectories."""
    all_files = []
    try:
        for dirpath, dirnames, filenames in os.walk(root_dir):
            for filename in filenames:
                all_files.append(os.path.join(dirpath, filename))
    except FileNotFoundError:
        print(f"Error: Root directory '{root_dir}' not found.")
    except PermissionError:
        print(f"Error: Permission denied to access '{root_dir}'.")
    return all_files

# Example usage:
root_directory = '.' # Start from the current directory
recursive_files = list_all_files_recursive(root_directory)
print(f"All files (recursive) in '{root_directory}':")
for f in recursive_files:
    print(f)

# Example with a specific path
# my_root_path = '/path/to/your/project'
# project_files = list_all_files_recursive(my_root_path)
# print(f"All files in '{my_root_path}': {project_files}")

Python code to recursively list all files in a directory tree using os.walk().

Using pathlib for Modern File System Operations

Python's pathlib module offers an object-oriented approach to file system paths, making code often more readable and robust. It provides Path objects that represent file system paths, with methods for common operations. The Path.iterdir() method is similar to os.listdir(), and Path.glob() or Path.rglob() can be used for pattern matching and recursive searches.

from pathlib import Path

def list_files_pathlib(directory_path):
    """Lists all files in a specified directory using pathlib."""
    path = Path(directory_path)
    files = []
    try:
        for entry in path.iterdir():
            if entry.is_file():
                files.append(entry.name)
    except FileNotFoundError:
        print(f"Error: Directory '{directory_path}' not found.")
    except PermissionError:
        print(f"Error: Permission denied to access '{directory_path}'.")
    return files

def list_all_files_recursive_pathlib(root_dir, pattern='*'):
    """Recursively lists all files matching a pattern using pathlib.rglob()."""
    path = Path(root_dir)
    all_files = []
    try:
        # rglob('*') lists all files and directories recursively
        # rglob('*.txt') lists all .txt files recursively
        for file_path in path.rglob(pattern):
            if file_path.is_file():
                all_files.append(str(file_path))
    except FileNotFoundError:
        print(f"Error: Root directory '{root_dir}' not found.")
    except PermissionError:
        print(f"Error: Permission denied to access '{root_dir}'.")
    return all_files

# Example usage:
current_directory = '.'
print(f"\nFiles in '{current_directory}' (pathlib): {list_files_pathlib(current_directory)}")

print(f"\nAll Python files (recursive) in '{current_directory}' (pathlib):")
python_files = list_all_files_recursive_pathlib(current_directory, '*.py')
for f in python_files:
    print(f)

Python code using pathlib for both single-directory and recursive file listing, with pattern matching.