How to Automatically List All Excel Files in Subfolders with Python

This guide shows how to use Python's os and glob modules to automatically locate every Excel file in nested subfolders, providing step‑by‑step code examples and explanations for extracting file paths and adding headers to each workbook.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
How to Automatically List All Excel Files in Subfolders with Python

1. Introduction

In the previous article a question was raised about handling Excel files located in multiple subfolders. This article answers that by demonstrating how to retrieve the paths of all Excel files using Python.

2. Implementation

The solution relies on the os and glob modules. First, import the modules:

import os
import glob

Then define the folder path, enumerate subfolders with os.scandir(), and collect Excel file paths using glob.glob():

import os
import glob

# Define folder path
folder_path = r"文件夹路径"

# Get all subfolder paths
subfolders = [f.path for f in os.scandir(folder_path) if f.is_dir()]

# Collect Excel file paths from each subfolder
excel_paths = []
for subfolder in subfolders:
    excel_paths.extend(glob.glob(subfolder + "/*.xlsx"))

The list excel_paths now contains the full paths of every Excel workbook found.

A more complete version adds pandas to read each workbook, set column headers, and save the changes:

import os
import pandas as pd
import glob

# Define folder path
folder_path = r"C:\Users\pdcfi\Desktop\新建文件夹"

# Get all subfolder paths
subfolders = [f.path for f in os.scandir(folder_path) if f.is_dir()]

excel_paths = []
for subfolder in subfolders:
    excel_paths.extend(glob.glob(subfolder + "/*.xlsx"))

for excel_file in excel_paths:
    df = pd.read_excel(excel_file, header=None)
    df.columns = ["经度", "纬度"]
    df.to_excel(excel_file, index=False)

This script adds the specified headers to every Excel file in the subfolders.

3. Summary

The article presents a practical Python solution for automating the retrieval and processing of Excel files across nested directories, helping users efficiently manage and modify their data files.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonosExcelglobfile-system
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.