Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

List Files in Python Directory



Among several file-handling operations in Python, the need to list all files in a directory is the most common one, and it's quite straightforward. Python offers a module named os that provides functions to work with the operating system, the directories, and the files seamlessly. In this article, we will cover ways of listing all files in a directory.

The following are the different methods used to list all files in a dictionary using Python -

  • os.listdir(): list all files in a directory
  • os.walk(): Recursively lists all files in subdirectories
  • pathlib.Path(): Object-oriented filesystem paths.
  • glob.glob(): Search for files and directories using Unix-style wildcards
  • os.scandir(): Returns an iterator of DirEntry objects

Using os.listdir() to List Files

To list all files in a directory, we can use the os.listdir() function.

Example

Let's understand the usage of the os.listdir to list all the files in a directory through an example.

  • Here, first we import the os module then list_files_in_directory() function takes directory_path as its parameter where we can specify the path of the directory that we want to list files from.
  • Using os.listdir(directory_path), we get a list of all files and directories present in the specified directory. Then, we filter out only the files from this list by checking each element's existence with os.path.isfile().
  • Finally, the function returns the list of files found in the directory. If the directory does not exist then we handle the FileNotFoundError and print an error message.
import os

def list_files_in_directory(directory_path):
   try:
      # Use os.listdir() to get a list of all files in the directory
      files_list = os.listdir(directory_path)

      # Filter out directories from the list, keeping only files
      files_list = [file for file in files_list if os.path.
isfile(os.path.join(directory_path, file))]

      return files_list

   except FileNotFoundError:
      print(f"Error: The directory '{directory_path}' does not exist.")
      return []

# Replace 'directory_path' with the path of the directory you want to list files from
directory_path = r'D:\Articles'
files_in_directory = list_files_in_directory(directory_path)

if files_in_directory:
   print("Files in the directory:")
   for file_name in files_in_directory:
      print(file_name)
else:
   print("No files found in the directory.")

Following is the output of the method os.listdir() to get the files in the directory -

Files in the directory:
How-do-I-list-all-files-of-a-directory-in-python.htm
sample.py
what-do-the-python-file-extensions-pyc-pyd-pyo-stand-for.htm
What-is-the-common-header-format-of-Python-files.htm

Using os.scandir() for Efficient Listing

The os.scandir() is one of the most efficient ways to list files and directories in Python. It returns an iterator of DirEntry objects by allowing fast access to file metadata such as file size, type, etc. without additional system.

Example

In this example, we will replace os.listdir() with os.scandir() to achieve a more productive listing of files in the directory by making use of os.scandir(directory_path) efficiently to iterate through the directory entries and there is no need to explicitly close the directory afterward.

It is checked if each entry in entries is a file by making use of entry.is_file(), and if it is found that the entry is a file, then we add its name to the files_list.

As done in the previous example, we notify the occurrence of the FileNotFoundError with a corresponding error message.

import os

def list_files_in_directory(directory_path):
   try:
      # Use os.scandir() for a more efficient listing
      with os.scandir(directory_path) as entries:
         files_list = [entry.name for entry in entries if entry.is_file()]

      return files_list

   except FileNotFoundError:
      print(f"Error: The directory '{directory_path}' does not exist.")
      return []

# Replace 'directory_path' with the path of the directory you want to list files from
directory_path = r'D:\templates'
files_in_directory = list_files_in_directory(directory_path)

if files_in_directory:
   print("Files in the directory:")
   for file_name in files_in_directory:
      print(file_name)
else:
   print("No files found in the directory.")

Here is the output of the method os.scandir() to get all the files in directory -

Files in the directory:
html_normal_page_template.htm
index_page_html_template.htm
reference_api_template_.htm

Recursive Listing with os.walk()

Python os.walk() is a generator function in the os module that allows us to traverse a directory tree, including all sub-directories and files.

Example

In this example, we utilize os.walk() to make a recursive list of files within sub-directories as well. The os.walk(directory_path) function gives as output a generator that has tuples containing the root directory, subdirectories and files within that directory.

Through each tuple iteration is done and for each file in the files list, the full file path is created using os.path.join() and the file is added to the files_list which makes it possible for us to obtain a complete list of all files including those present in subdirectories as well.

import os

def list_files_in_directory(directory_path):
   try:
      # Use os.walk() to get a recursive listing of all files
      files_list = []
      for root, dirs, files in os.walk(directory_path):
         for file in files:
            files_list.append(os.path.join(root, file))

      return files_list

   except FileNotFoundError:
      print(f"Error: The directory '{directory_path}' does not exist.")
      return []
# Replace 'directory_path' with the path of the directory you want to list files from
directory_path = r'D:\templates'
files_in_directory = list_files_in_directory(directory_path)

if files_in_directory:
   print("Files in the directory:")
   for file_name in files_in_directory:
      print(file_name)
else:
   print("No files found in the directory.")

Following is the output of the method os.walk() -

Files in the directory:
Files in the directory:
D:\templates\html_normal_page_template.htm
D:\templates\index_page_html_template.htm
D:\templates\reference_api_template_.htm

Using pathlib.Path() for Modern Listing

The pathlib is a built-in module since Python 3.4 which offers an object-oriented interface to handle filesystem paths. It's a modern alternative to the older os.path, os, and glob modules.

Example

The pathlib.Path(directory_path) helps to create a Path object, which allows us to work with the directory and its contents, and we get an iterator of all directory entries, i.e., files and directories, by making use of path.iterdir() function.

While using file.is_file() we make sure to check if each entry is a file and if true then we extract its name and add it to the files_list.

from pathlib import Path
def list_files_in_directory(directory_path):
   try:
      # Use pathlib.Path() for modern file listing
      path = Path(directory_path)
      files_list = [file.name for file in path.iterdir() if file.is_file()]

      return files_list

   except FileNotFoundError:
      print(f"Error: The directory '{directory_path}' does not exist.")
      return []
# Replace 'directory_path' with the path of the directory you want to list files from
directory_path = r"D:\Articles"
files_in_directory = list_files_in_directory(directory_path)

if files_in_directory:
   print("Files in the directory:")
   for file_name in files_in_directory:
      print(file_name)
else:

   print("No files found in the directory.")

Here is the output of the method pathlib.path() -

Files in the directory:
Files in the directory:
How-do-I-list-all-files-of-a-directory-in-python.htm
sample.py
what-do-the-python-file-extensions-pyc-pyd-pyo-stand-

Using glob.glob() to Match Files with Patterns

The glob module finds all pathnames matching a specified pattern using wildcards similar to Unix shell rules such as *, ?, etc.

Here are the patterns used in the glob.glob() method to find the files -

ID Pattern Description
1 * Matches any number of characters excluding directory separators.
2 ? Matches exactly one character.
3 [abc] Matches a single character: either 'a', 'b', or 'c'.
4 [a-z] Matches any lowercase character from 'a' to 'z'.
5 [!abc] Matches any character except 'a', 'b', or 'c'.
6 *.txt Matches all files ending with '.txt'.
7 file?.py Matches files like 'file1.py', 'fileA.py' but not 'file12.py'.
8 ** Matches files in the current directory and all subdirectories, when used with recursive=True.

Example to Match All Files in a Folder

In this example, we use the wildcard pattern * to get all the files in a defined directory or folder -

import glob

files = glob.glob(r"D:\Articles\*")
print(files)

Here is the output of the above code -

['D:\Articles\test1.htm', 'D:\Articles\sample.py', 'D:\Articles\test2.htm', 'D:\Articles\test3.htm']

Example to Match Only .py Files

In this example, we use the wildcard pattern *.py to get all the Python files in a specific directory -

import glob 
python_files = glob.glob(r"D:\Articles\*.py") 
print(python_files) 

Here is the output of the above code -

['D:\Articles\sample.py']

Match Multiple Extensions

In this example, we use a character set wildcard pattern *.[jt]s to match JavaScript and TypeScript files -

import glob 
files = glob.glob(r"D:\Articles\*.[jt]s") 
print(files) 

Here is the output of the above code -

['D:\Articles\script.js', 'D:\Articles\types.ts'] 
Updated on: 2025-04-17T17:37:45+05:30

739 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements