Python
Coding
File Handling
Directory Search
Text Files

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

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In Python, working with files and directories is a common task, particularly when dealing with large datasets or when performing file manipulation tasks. When you need to find all the files in a directory with a specific extension (e.g., .txt), Python's standard library provides several tools to assist you. The most commonly used modules for interacting with the filesystem are os and pathlib. Here, we'll explore how to use these modules to find all .txt files in a specified directory.

Using the os Module

The os module includes versatile utilities to interact with the operating system. os.listdir() and os.path are particularly useful for directory and file manipulation.

Example with os:

python
1import os
2
3def find_txt_files(directory):
4    txt_files = []
5    for filename in os.listdir(directory):
6        if filename.endswith(".txt"):
7            txt_files.append(os.path.join(directory, filename))
8    return txt_files
9
10# Usage
11directory_path = '/path/to/directory'
12txt_files = find_txt_files(directory_path)
13print(txt_files)

This code lists all files in the specified directory, checks if they end with the .txt extension, and adds them to the txt_files list.

Using the pathlib Module

Introduced in Python 3.4, pathlib offers a more intuitive approach to handle filesystem paths. It provides an object-oriented way to manage file system paths.

Example with pathlib:

python
1from pathlib import Path
2
3def find_txt_files(directory):
4    path = Path(directory)
5    return [str(file) for file in path.glob('*.txt')]
6
7# Usage
8directory_path = '/path/to/directory'
9txt_files = find_txt_files(directory_path)
10print(txt_files)

Path.glob() method is used for pattern matching, and *.txt effectively finds all .txt files in the directory.

Comparing os and pathlib

Below is a table comparing the use of os and pathlib for finding .txt files in a directory:

Featureos Modulepathlib Module
ApproachProceduralObject-Oriented
Line of codeMoreLess
ReadabilityLowerHigher
Pattern MatchingManual checkBuilt-in with glob method

Additional Considerations

  • Recursively Finding Files: To find files not only in the specified directory but also in all subdirectories, you can modify the glob method in pathlib to **/*.txt or use os.walk() in the os module.
  • Error Handling: It's good practice to add error handling when dealing with file operations to handle situations like permission errors or non-existent directories.
  • Performance: For directories with a very large number of files, consider performance implications of each approach.

Conclusion

Both os and pathlib provide robust solutions for locating files with a specific extension in Python, with pathlib generally offering a more modern and easy-to-use API. The choice between using os or pathlib can depend on personal preference or specific project requirements regarding readability and code maintainability.


Course illustration
Course illustration

All Rights Reserved.