Finding File Names in a Directory

  • Share this:

Code introduction


This function takes a directory path as an argument and returns a list of names of all files in the directory.


Technology Stack : os, re

Code Type : Function

Code Difficulty : Intermediate


                
                    
import os
import re

def find_files_in_directory(directory):
    """
    This function finds all files in the given directory and returns their names.

    :param directory: str, the path to the directory to search in
    :return: list of str, the names of the files in the directory
    """
    return [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]                
              
Tags: