How to find all files in a directory with extension .txt in Python?


Hunting for specific files in a directory is a task that can be effortlessly accomplished using tools in Python; in some situations, you may need to find all the files in a directory with an extension .txt using Python. Let's take a deep dive into the process involved in this task and present you different ways how this task of finding all files with the .txt extension in a directory can be achieved with easy-to-follow code examples along with explanations.

Using os.listdir()

In this code example, we start by importing the os module, which is essential for working with directories and files in Python.

Example

The find_txt_files() function takes directory_path as its parameter; directory_path represents the path of the directory you wish to search.

We use os.listdir(directory_path) to obtain a list of all items, i.e., files and directories in the specified directory.

By applying iteration over each item and checking if it is a file by using os.path.isfile(), we make it sure that we only consider files and not directories.

In the second condition, we make use of item.endswith('.txt') to retrieve only the files with the .txt extension.

The function gives a list of text files found in the directory as the output.

import os

def find_txt_files(directory_path):
   try:
      # Use os.listdir() to obtain a list of all items in the directory
      all_items = os.listdir(directory_path)

      # Filter out only the files with '.txt' extension
      txt_files = [item for item in all_items if 
os.path.isfile(os.path.join(directory_path, item)) and item.endswith('.
txt')]

      return txt_files

   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 
search
directory_path = '/path/to/your/directory'
txt_files_list = find_txt_files(directory_path)

if txt_files_list:
   print("Text files in the directory:")
   for file_name in txt_files_list:
      print(file_name)
else:
   print("No .txt files found in the directory.")

Output

For a certain directory, the following output was obtained

Text files in the directory:
fubar.txt

Using os.listdir()

Example

In this present example, we begin by importing the os module, which enables us to interact with the operating system, the directories, and the files.

The find_txt_files() function accepts directory_path as its parameter. The directory_path represents the path of the directory you wish to search for .txt files.

The os.listdir(directory_path) function is deployed to obtain a list of all items, that is, the files and the directories in the specified directory.

By iterating over each item and checking if it is a file using os.path.isfile(), we make sure we only consider the files and ignore the directories.

In the second instance, we deploy item.endswith('.txt') to seek out only the files with the .txt extension.

The function is found to return a list of .txt files found in the directory.

import os

def find_txt_files(directory_path):
   try:
      # Get a list of all items (files and directories) in the specified 
directory
      all_items = os.listdir(directory_path)

      # Filter out only the files with the '.txt' extension
      txt_files = [item for item in all_items if os.path.isfile(os.path.
join(directory_path, item)) and item.endswith('.txt')]

      return txt_files

   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 
search
directory_path = '/path/to/your/directory'
txt_files_list = find_txt_files(directory_path)

if txt_files_list:
   print("Text files in the directory:")
   for file_name in txt_files_list:
      print(file_name)
else:
   print("No .txt files found in the directory.")

Output

For a certain directory, the following output was obtained

Text files in the directory:
fubar.txt

Using os.scandir() for Efficiency

Example

Here, os.listdir() is replaced with os.scandir() so as to provide a more efficient way to list files in the directory.

When the output of os.scandir(directory_path) is used as a list of entries, a context manager is created that is found to efficiently iterate through the directory entries and there is no need to explicitly close the directory afterward.

By using entry.is_file(), it is checked if each entry is a file, and if found to be a file, we proceed to check if it ends with .txt.

The function is found to return a list of .txt files found in the directory.

import os

def find_txt_files(directory_path):
   try:
      # Use os.scandir() for a more efficient listing
      with os.scandir(directory_path) as entries:
         txt_files = [entry.name for entry in entries if entry.is_file() 
and entry.name.endswith('.txt')]

      return txt_files

   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 
search
directory_path = '/path/to/your/directory'
txt_files_list = find_txt_files(directory_path)

if txt_files_list:
   print("Text files in the directory:")
   for file_name in txt_files_list:
      print(file_name)
else:
   print("No .txt files found in the directory.")

Output

For a certain directory, the following output was obtained

Text files in the directory:
fubar.txt

Recursive Search with os.walk()

Example

In this particular example, we employ os.walk() to achieve a recursive search for .txt files, that includes subdirectories.

Then os.walk(directory_path) function returns a generator that goes on to provide tuples containing the root directory, subdirectories, and files within that directory.

Each tuple is iterated through, and for each file in the files list, we deploy file.endswith('.txt') to find if it ends with .txt extension.

If it does end with that extension, we construct the full file path by using os.path.join(root, file) and add that file to the txt_files list.

The function finally returns a comprehensive list of .txt files found within the directory and its subdirectories.

import os

def find_txt_files(directory_path):
   try:
      # Use os.walk() to get a recursive listing of all files
      txt_files = []
      for root, dirs, files in os.walk(directory_path):
         for file in files:
            if file.endswith('.txt'):
               txt_files.append(os.path.join(root, file))

      return txt_files

   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 
search
directory_path = '/path/to/your/directory'
txt_files_list = find_txt_files(directory_path)

if txt_files_list:
   print("Text files in the directory:")
   for file_name in txt_files_list:
      print(file_name)
else:
   print("No .txt files found in the directory.")

Output

For a certain directory, the following output was obtained

Text files in the directory:
/content/foo/fubar.txt

Using pathlib.Path() for Modern Listing

Example

In this final example, we take up the latest and modern approach for performing the same task of listing by using pathlib.Path().

We initially import Path from the pathlib module; this module provides an object-oriented interface for working with directories and files.

A Path object is created by Path(directory_path) pointing to the specified directory.

An iterator of all entries including files and directories is created by using path.iterdir() to obtain the same within the directory.

The file.is_file() function is used to check if each entry is a file, and if it is indeed a file, we check if it has a .txt suffix by using file.suffix.

If all these conditions are met, the file is included in the txt_files list.

The function is then found to return a list of .txt files found in the directory.

from pathlib import Path

def find_txt_files(directory_path):
    try:
        # Use pathlib.Path() for modern file listing
        path = Path(directory_path)
        txt_files = [file for file in path.iterdir() 
if file.is_file() and file.suffix == '.txt']

        return txt_files

    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 search
directory_path = '/path/to/your/directory'
txt_files_list = find_txt_files(directory_path)

if txt_files_list:
    print("Text files in the directory:")
    for file_name in txt_files_list:
        print(file_name)
else:
    print("No .txt files found in the directory.")

Output

For a certain directory, the following output was obtained

Text files in the directory:
/content/foo/fubar.txt

There you name it and you have it—four diverse and efficient methods to find all files with the .txt extension in a directory using Python. You always have the choice of selecting one or more of the several ways such as the classic os.listdir(), the efficient os.scandir(), the recursive os.walk(), or the modern pathlib.Path(), to cater to your specific needs. Having learned these code examples and explanations, you now have a versatile toolkit to automate file searches and organize your Python projects with elegance and ease.

With the practice of these concise and elegant code snippets, you can effortlessly locate all files with the .txt extension in any directory. Python's versatility and ease of use make it a fantastic choice for handling file-related tasks, whether you're managing data, organizing files, or processing text files for analysis.

Updated on: 28-Jul-2023

675 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements