How to Loop Through Folders and Subfolders in Python
To loop through the files in a folder with Python, use pathlib: Path(“folder”).glob(“*.xlsx”) returns the matching files in that folder, and rglob(“*.xlsx”) also searches every subfolder. os.walk() is the older alternative that yields each folder with its files. Then loop over the paths and open each file with pandas or openpyxl.
Loop through a folder
Use glob on a pathlib Path for one folder and rglob to include all subfolders. Both take a pattern, so "*.xls*" returns .xlsx, .xlsm and .xls files and skips everything else. Each result is a Path object you can open directly.
The examples use a folder called Excel_Files next to the script. It holds seven Excel files plus subfolders with more of them, so glob returns the seven and rglob returns every file.

from pathlib import Path
input_dir = Path.cwd() / "Excel_Files"
# Files in this folder only
files = list(input_dir.glob("*.xls*"))
# Files in this folder and every subfolder
all_files = list(input_dir.rglob("*.xls*"))
for path in all_files:
print(path.name, path.parent)
If you prefer the os module, os.walk gives you the same result. It yields each folder once, together with the names of the subfolders and files inside it.
import os
for folder, subfolders, filenames in os.walk("Excel_Files"):
for filename in filenames:
if filename.endswith((".xlsx", ".xlsm", ".xls")):
print(os.path.join(folder, filename))
I use pathlib in new code because the paths are objects with handy attributes like .name, .stem and .suffix. My pathlib quickstart lists the ones I use most. Both snippets were tested with Python 3.12 in November 2026.
Watch out: Excel creates hidden lock files named like ~$Report.xlsx while a workbook is open. The pattern matches them too, and pandas fails to read them. Close the workbooks first or skip names that start with ~$.
Merge every file into one
To combine all files into one master workbook, read each one into a pandas dataframe, collect them in a list, and join them with pd.concat. The files in the example share one layout, except that the files from 1990 to 1996 have no income column.
The script creates the MasterFile folder if it does not exist and writes the result without the dataframe index:
import pandas as pd # pip install pandas openpyxl
parts = []
for path in input_dir.rglob("*.xls*"):
part = pd.read_excel(path)
parts.append(part)
df = pd.concat(parts)
output_dir = Path.cwd() / "MasterFile"
output_dir.mkdir(exist_ok=True)
df.to_excel(output_dir / "masterfile.xlsx", index=False)
The master file contains every year. pandas keeps the income column, and rows from the files without it get an empty cell there.

If the files use different column names for the same data, see how to merge Excel files with different headers.
Change a cell in every file
To edit the files instead of merging them, open each one with openpyxl, change it and save it. The example writes “New ID” into cell A1 of Sheet1 and saves every file to a New ID folder, so the originals stay untouched.
from openpyxl import load_workbook # pip install openpyxl
output_dir = Path.cwd() / "New ID"
output_dir.mkdir(exist_ok=True)
for path in input_dir.rglob("*.xls*"):
wb = load_workbook(filename=path)
ws = wb["Sheet1"]
ws["A1"] = "New ID"
wb.save(output_dir / path.name)

The same loop works for bigger edits such as formulas, formatting or charts. I use it to find and replace values in multiple Excel files and to convert all formulas to values. One limit: openpyxl only opens .xlsx and .xlsm files, not old .xls files.
