Finding File Paths in Directory and Subdirectories

  • Share this:

Code introduction


This function searches for all files in the specified directory and its subdirectories and returns a list of file paths.


Technology Stack : os, os.walk

Code Type : File search

Code Difficulty : Intermediate


                
                    
import os
import re

def find_files_in_directory(directory_path):
    """
    This function finds all files in the specified directory and its subdirectories.

    :param directory_path: The path to the directory in which to search for files.
    :return: A list of file paths found in the directory and its subdirectories.
    """
    files = []
    for root, dirs, filenames in os.walk(directory_path):
        for filename in filenames:
            files.append(os.path.join(root, filename))
    return files                
              
Tags: