List Files

import os
import fnmatch

def find_files(directory, extension):
    """
    Walks through a directory and finds all files with a given extension.

    Parameters:
    directory (str): The directory to search in.
    extension (str): The file extension to search for.

    Returns:
    list: A list of paths to the files matching the given extension.
    """
    matching_files = []

    for root, dirs, files in os.walk(directory):
        for file in files:
            if fnmatch.fnmatch(file, f"*.{extension}"):
                matching_files.append(os.path.join(root, file))

    return matching_files

# Example usage
directory_to_search = "/path/to/your/directory"
file_extension = "txt"  # Replace with the desired file extension
found_files = find_files(directory_to_search, file_extension)

for file in found_files:
    print(file)


GPT explanation

The os.walk(directory) function in Python is a generator that yields a tuple of three values for each directory in the tree rooted at the directory you pass to it. These three values are:

  1. root (str): The current directory path it is iterating over. This is the path to the directory from which os.walk starts its traversal down into its subdirectories.

  2. dirs (list): A list of names of the subdirectories in root (excluding . and .. if they are present). These are the directories that os.walk will explore in future iterations. It's important to note that modifying this list will influence the directories that are walked into by os.walk.

  3. files (list): A list of names of the non-directory files in root. These are the actual file names within the directory root. They are not full paths but just names, so if you need a full path (which is common), you have to join them with the root using os.path.join(root, file).

This structure is useful because it allows you to iterate through the directory structure, performing actions on each file and knowing its directory context (from the root), while also having the ability to see which subdirectories exist in each directory (from dirs), if you need to perform some action on these or alter the course of the walk.


Backlinks