How to read multiple text files from folder in Python?
Reading multiple text files from a folder in Python means accessing all the .txt files stored within a specific directory and processing their contents one by one. For example, if a folder contains three text files, each with a single line of text, you might want to read each file’s content and process or display it efficiently. Let's understand different methods to do this efficiently.
Below is our folder structure, which will be used in the methods demonstrated:
example_folder/
├── file1.txt
├── file2.txt
└── file3.txt
Each .txt file contains exactly one line of text:
- file1.txt contains:
This is the first file.
- file2.txt contains:
This is the second file.
- file3.txt contains:
This is the third file.
Using os.scandir()
This method lets you look inside a folder and quickly check each item. You can tell which ones are files, then open and read only the text files. It’s a fast way to explore a folder and pick out the files you want.
import os
p = r"C:\Users\GFG0578\Desktop\example_folder"
with os.scandir(p) as es:
for e in es:
if e.is_file() and e.name.endswith('.txt'):
with open(e.path, encoding='utf-8') as f:
print(f.read().strip())
Output
This is the first file.
This is the second file.
This is the third file.
Explanation: os.scandir(p) iterates over folder entries, filters files ending with .txt, reads each file with UTF-8 encoding, strips whitespace and prints the content.
Using pathlib.Path.glob()
This is a neat and modern way to find files that match a pattern, like all .txt files. You get easy-to-use file objects, and you can read their contents with just one command. It makes working with files simple and clean.
from pathlib import Path
p = Path(r"C:\Users\GFG0578\Desktop\example_folder")
for f in p.glob('*.txt'):
print(f.read_text(encoding='utf-8').strip())
Output
This is the first file.
This is the second file.
This is the third file.
Explanation: Create a Path object for the folder and use .glob('*.txt') to find all text files. For each file, read its UTF-8 content, strip whitespace and print it.
Using glob.glob()
Think of this like searching your folder for all files that end with .txt. It gives you a list of matching file paths, which you can then open and read one by one. It’s straightforward and works well if you want to find files by name.
import glob, os
d = r"C:\Users\GFG0578\Desktop\example_folder"
for p in glob.glob(os.path.join(d, '*.txt')):
with open(p, encoding='utf-8') as f:
print(f.read().strip())
Output
This is the first file.
This is the second file.
This is the third file.
Explanation: glob.glob() searches the folder for files that end with .txt and returns a list of matching file paths. You then loop through this list, open each file, read its content and print the cleaned (whitespace-stripped) content.
Using os.listdir()
This method just grabs a list of everything inside a folder in which files and folders mixed together. You then check each item’s name to find the text files, and read them. It’s the simplest way to see what’s inside a folder.
import os
d = r"C:\Users\GFG0578\Desktop\example_folder"
for fn in os.listdir(d):
if fn.endswith('.txt'):
with open(os.path.join(d, fn), encoding='utf-8') as f:
print(f.read().strip())
Output
This is the first file.
This is the second file.
This is the third file.
Explanation: os.listdir() returns a list of all files and folders in the directory. You then filter this list to select only the files that end with .txt. Finally, you open and read the content of each matching file.