Find all files in a directory with extension .txt in Python

Problem: Given a directory consisting of different file types, how to find all the files in the directory with the.txt extension?

As a developer, you may need to prune the type of files present within a certain folder, i.e., list the files in a directory having a specific extension before performing any operation on them. For example, .txt extension files are used when you want to copy the text files from one location to another. In such cases, we have to first find all the files in the directory having a .txt extension and then list them or use them accordingly. In this article, we are going to learn different methods to find all the files in a directory with the extension .txt in Python.

Note – This tutorial will guide you to locate and list files with .txt extension, but you can apply these methods to search and list files with any extension (.csv, .pdf, .jpg, etc.) within a directory.

๐Ÿง‘โ€๐Ÿ’ป Recommended: Python Get All TXT Files in a Folder

Without further delay, let us dive into the solutions to our mission-critical question.

Video Walkthrough

Method 1: Using The “os” Module:

The os module in Python has various functions which can be used to work with directories and system files. To use the os module and its functions, you have to first import it into your program:

import os

1.1 os.listdir()

The os.listdir() method from the os module in Python lists all the files inside a specified directory. The list of files in the current working directory gets returned if no directory is specified.

Syntax: 

os.listdir(path)

We have to use the .endswith() method along with the os.listdir() method to return the text files from the directory. It filters all the files based on the provided extensions. We can use a “if” condition to check if the filename ends with a .txt extension.

Let’s suppose the following text files – “demo.txt”,”work.txt”,”abc.txt” are present in the current working directory.

Example:

# Importing the os module
import os
l = []
print("All the text files from directory:")
for f in os.listdir("."):
    # Printing only text files from directory
    if f.endswith('.txt'):
        l.append(f)
for file in l:
    print(file)

Output:

All the text files from directory:
abc.txt
demo.txt
work.txt

1.2 os.walk()

os.walk() is a recursive function of the os module in Python. When it gets called, it creates a tuple of values that consists of the current path, the directories in the current path, and the files in the current path. The os.walk() method follows each directory recursively and returns the list of files and directories until there are no further subdirectories available from the initial directory.

Syntax: 

os.walk(path)

Example:

import os

print("All the text files from directory:")
# Printing only text files from directory
for root, dirs, files in os.walk('.'):
    for file in files:
        if file.endswith('.txt'):
            print(file)

Output:

All the text files from directory:
abc.txt
demo.txt
work.txt

1.3 os.scandir()

The os.scandir() method got introduced in Python 3.5 and is one of the latest methods in Python used to list all the files in a directory. This method returns an iterator instead of the list.

Example:

import os

lst = [text_file.name for text_file in os.scandir('.') if text_file.name.endswith('.txt')]
print("All the text files in the directory:")
for files in lst:
    print(files)

Output:

All the text files in the directory:
abc.txt
demo.txt
work.txt

Method 2: Using The glob() module

The glob module is a module in Python that has a few built-in functions that can help in listing files of a specified directory. To use the glob module and its functions, you have to first import it into your program. As it is a built-in module in Python, we do not have to install it externally but it is necessary to import it.

2.1 glob.glob()

glob.glob() is a method from the glob module in Python that provides wildcards like “*”, “?”, [ranges]  that make the process of retrieval of path very easy. The “*” character helps to match all the specified items (In this case, the text files) in the current directory.

Syntax:

glob.glob(path, wildcard)

Example:

# Importing the glob module
import glob
# Storing only text files from directory
path = '*.txt'
print("All the text files from directory:")
for file in glob.glob(path):
    print(file)

Output:

All the text files from directory:
abc.txt
demo.txt
work.txt

2.2 glob.iglob() method:

The glob.iglob() method from the glob module in Python is used to print the files recursively. To do this, we have to set the recursive parameter to be True. The ** command is used to search the files recursively.

Syntax:

glob.iglob(path, wildcard, recursive = True)

Example:

# Importing the glob module
import glob

path = '**/*.txt'

files = glob.iglob(path, recursive=True)
print("All the text files from directory:")
# Printing only text files from the directory
for file in files:
    print(file)

Output:

All the text files from directory:
abc.txt
demo.txt
work.txt

Method 3: Using The pathlib Module

The pathlib module is used to represent file system paths appropriate for different operating systems. We can use the pathlib modules glob method to find all files in a directory with extension .txt in Python. To do this we need to import the pathlib module first:

Import pathlib

Caution: In Python 3.4 the pathlib module got included in the standard library. However, we can install the back-ports of that module even on the older Python versions by using conda or pip: pathlib and pathlib2.

Example:

# Importing the pathlib module
import pathlib

print("All the text files from directory:")
# Printing only text files from directory
for file in pathlib.Path('.').glob('*.txt'):
    print(file)

Output:

All the text files from directory:
abc.txt
demo.txt
work.txt

Method 4: Using fnmatch

fnmatch is a module in Python that compares a single file name against a pattern. It returns True if they match. Otherwise, it returns False. You must import the fnmatch module into your program before utilizing it.

Syntax:

fnmatch.fnmatch(filename, pattern)

Example:

# Importing the fnmatch and os module
import fnmatch
import os

print("All the text files from directory:")
# Printing only text files from directory
for file in os.listdir('.'):
    if fnmatch.fnmatch(file, '*.txt'):
        print(file)

Output:

All the text files from directory:
abc.txt
demo.txt
work.txt

Conclusion

We looked at the different methods to find all files in a directory with extension .txt. I hope this article has helped you. Please stay tuned and subscribe for more solutions and interesting discussions in the future. Happy coding!

Recommended Read: How Do I List All Files of a Directory in Python?


Python Regex Course

Google engineers are regular expression masters. The Google search engine is a massive text-processing engine that extracts value from trillions of webpages.ย ย 

Facebook engineers are regular expression masters. Social networks like Facebook, WhatsApp, and Instagram connect humans via text messages.ย 

Amazon engineers are regular expression masters. Ecommerce giants ship products based on textual product descriptions.ย ย Regular expressions โ€‹rule the game โ€‹when text processing โ€‹meets computer science.ย 

If you want to become a regular expression master too, check out the most comprehensive Python regex course on the planet: