Combining many files into one

Loop through a folder of Excel files, read each one, label where it came from, and stack them into one table.

Module 5: Automating the boring work · Lesson 14 of 18 · 19 min read · Free

Back to Halima's Monday from lesson 1.1: six branches each send an Excel file, and someone has to put them together. This lesson automates exactly that.

py-branch-reports.zip holds six files, one per branch, with June's orders. Download it and upload it to Colab.

Unzip the files

Python can open zip files itself:

import zipfile

with zipfile.ZipFile("py-branch-reports.zip") as z:
    z.extractall()
    print(z.namelist())
['branch-reports/abuja-june-2026.xlsx', 'branch-reports/ibadan-june-2026.xlsx', 'branch-reports/ikeja-june-2026.xlsx', 'branch-reports/kano-june-2026.xlsx', 'branch-reports/lekki-june-2026.xlsx', 'branch-reports/port-harcourt-june-2026.xlsx']

with opens the zip and closes it again when the indented block ends. The files are now in a folder called branch-reports. (In Colab you can also run !unzip py-branch-reports.zip in a cell. The ! runs a command on Colab's computer.)

Find the files with pathlib

pathlib is Python's built-in toolkit for files and folders. glob("*.xlsx") finds every file whose name ends in .xlsx:

from pathlib import Path

folder = Path("branch-reports")
files = sorted(folder.glob("*.xlsx"))
for f in files:
    print(f.name)
abuja-june-2026.xlsx
ibadan-june-2026.xlsx
ikeja-june-2026.xlsx
kano-june-2026.xlsx
lekki-june-2026.xlsx
port-harcourt-june-2026.xlsx

Read one file and look at it

Always look at one file before looping through all of them:

import pandas as pd

one = pd.read_excel(files[0])
print(one.shape)
one.head(3)
(32, 10)
   order_id        date          rep            customer                     product   category  qty  unit_price   payment status
0  KD-10754  2026-06-02   Peter Udoh   Amaka Supermarket  Detergent 1kg (pack of 10)  Household   10       16200  Transfer   Paid
1  KD-10764  2026-06-04  Hauwa Bello       Femi Ventures    Spaghetti (carton of 20)      Pasta    3       17800       POS   Paid
2  KD-10765  2026-06-04   Peter Udoh  Alhaji Musa Stores  Bottled water (pack of 12)  Beverages   10        2500  Transfer   Paid

There's a catch. The branch files don't have a branch column, because each branch only sends its own orders. Once the files are stacked together, you won't be able to tell which row came from where, so you'll take the branch from the file name.

f = files[-1]
print(f.stem)
f.stem.replace("-june-2026", "").replace("-", " ").title()
port-harcourt-june-2026
'Port Harcourt'

f.stem is the file name without the .xlsx ending.

Loop, label and combine

Six branch files become one table

Read each file, add its branch, and collect the tables in a list. Then pd.concat() stacks them into one:

tables = []
for f in files:
    table = pd.read_excel(f)
    table["branch"] = f.stem.replace("-june-2026", "").replace("-", " ").title()
    tables.append(table)

june = pd.concat(tables, ignore_index=True)
print(june.shape)
june["branch"].value_counts()
(151, 11)
branch
Kano             39
Abuja            32
Ibadan           25
Ikeja            19
Port Harcourt    19
Lekki            17
Name: count, dtype: int64

ignore_index=True numbers the rows 0, 1, 2… again, instead of keeping each file's own numbering.

Check it

When combining files, check that nothing was lost: the combined rows should equal the rows in all the files added together.

print(sum(len(t) for t in tables) == len(june))
june["date"].min(), june["date"].max()
True
('2026-06-01', '2026-06-30')

Check yourself

Answer in your head first, then open each question to see the answer and why.

  1. What does Path("branch-reports").glob("*.xlsx") find?

    • A Every file ending in .xlsx in the branch-reports folder
    • B Only the first Excel file
    • C Every file on the computer
    • D The folder's size
    Show the answer

    A) Every file ending in .xlsx in the branch-reports folder — * matches any name, so *.xlsx matches every Excel file.

  2. What does pd.concat(tables, ignore_index=True) do?

    • A Stacks a list of tables into one and renumbers the rows
    • B Joins columns side by side by customer
    • C Deletes duplicate files
    • D Saves each table as a file
    Show the answer

    A) Stacks a list of tables into one and renumbers the rows — concat stacks tables on top of each other; ignore_index renumbers the rows.

  3. The branch files have no branch column. How does the lesson know which branch each row came from?

    • A It reads the branch from each file's name and adds it as a column
    • B pandas guesses
    • C It asks the user for every row
    • D It can't be done
    Show the answer

    A) It reads the branch from each file's name and adds it as a column — f.stem gives the file name, which is turned into a branch name.

More free lessons: What Python can do for your work · Lists and loops · Loading data and taking a first look · Fixing text and numbers stored as text · Using AI coding assistants wisely