Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to find all files in a directory with extension .txt in Python?
Searching for specific files in a directory is a common task that can be accomplished using different tools in Python.
In this article, we will see how to find all the files in a directory with extension .txt using Python. Here are different approaches, let's see each one in detail ?
Using os.listdir()
The os.listdir() function from Python's built-in os module returns a list of file and directory names in the given directory path.
Example
In this example, we use os.listdir() with list comprehension to filter files ending with ".txt" ?
import os
# Create a test directory structure
os.makedirs("test_dir", exist_ok=True)
with open("test_dir/file1.txt", "w") as f:
f.write("Hello World")
with open("test_dir/file2.txt", "w") as f:
f.write("Python Tutorial")
with open("test_dir/readme.md", "w") as f:
f.write("# Documentation")
# Find all .txt files
directory = "test_dir"
txt_files = [file for file in os.listdir(directory) if file.endswith(".txt")]
print("Text files in the directory:", txt_files)
Text files in the directory: ['file1.txt', 'file2.txt']
Using os.scandir()
The os.scandir() is a more efficient alternative to os.listdir(), especially when you need file metadata or want to filter by file type.
Example
Here we use os.scandir() with the is_file() method to ensure we only get files, not directories ?
import os
# Using the same test directory from previous example
directory = "test_dir"
txt_files = [entry.name for entry in os.scandir(directory)
if entry.is_file() and entry.name.endswith(".txt")]
print("Text files in the directory:", txt_files)
Text files in the directory: ['file1.txt', 'file2.txt']
Recursive Search with os.walk()
The os.walk() function allows us to traverse a directory tree recursively, searching through all subdirectories.
Example
In this example, we use os.walk() to find .txt files in the directory and all subdirectories ?
import os
# Create nested directory structure
os.makedirs("test_dir/subdir", exist_ok=True)
with open("test_dir/subdir/nested.txt", "w") as f:
f.write("Nested file")
directory = "test_dir"
txt_files = []
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(".txt"):
txt_files.append(os.path.join(root, file))
print("Text files found:")
for file in txt_files:
print(file)
Text files found: test_dir/file1.txt test_dir/file2.txt test_dir/subdir/nested.txt
Using pathlib.Path()
The pathlib.Path provides a modern object-oriented approach to work with filesystem paths. The rglob() method performs recursive pattern matching.
Example
In this example, we use Path.rglob() method for recursive search with pattern matching ?
from pathlib import Path
directory = Path("test_dir")
txt_files = list(directory.rglob("*.txt"))
print("Text files found:")
for file in txt_files:
print(file)
Text files found: test_dir/file1.txt test_dir/file2.txt test_dir/subdir/nested.txt
Using glob.glob()
The glob.glob() function finds pathnames matching a specified pattern according to Unix shell rules. It supports recursive searches with the ** wildcard.
Example
In this example, we use glob.glob() with recursive flag to search through all subdirectories ?
import glob
pattern = "test_dir/**/*.txt"
txt_files = glob.glob(pattern, recursive=True)
print("Text files found:")
for file in txt_files:
print(file)
Text files found: test_dir/file1.txt test_dir/file2.txt test_dir/subdir/nested.txt
Comparison
| Method | Recursive | Performance | Best For |
|---|---|---|---|
os.listdir() |
No | Fast | Simple directory listing |
os.scandir() |
No | Fastest | When file metadata is needed |
os.walk() |
Yes | Good | Complex directory traversal |
pathlib.Path() |
Yes | Good | Modern, clean syntax |
glob.glob() |
Yes | Good | Pattern-based file matching |
Conclusion
Python offers multiple powerful methods to find .txt files in directories. Use os.scandir() for single directories, pathlib.Path.rglob() for modern recursive searches, or glob.glob() for pattern matching across directory trees.
