How to Create a list of files, folders, and subfolders in Excel using Python?

Python is a great programming language widely used for various data manipulation tasks. When working with files and folders, it can be useful to generate a list of all the files, folders, and subfolders within a directory. Excel, on the other hand, is a popular spreadsheet application that allows users to organize and analyze data. In this detailed article, we will explore step−by−step how to use Python to create a comprehensive list of files, folders, and subfolders in Excel, providing a convenient way to manage and analyze file structures. So make sure to stick with this till the end.


To follow along with this tutorial, you will need to have Python installed on your computer, as well as the pandas library, which is commonly used for data manipulation tasks in Python. Additionally, a basic understanding of Python syntax and file operations will be helpful.

Step 1: Importing the Required Libraries

First, let's start by importing the necessary libraries: os and pandas. The os library provides functions for interacting with the operating system, while pandas is a powerful data manipulation library widely used in Python.

import os
import pandas as pd

Step 2: Defining the Directory Path

The directory path for which we wish to construct the list of files, folders, and subfolders must then be specified. Depending on your needs, you can either offer an absolute path or a relative path.

directory_path = "C:/Path/To/Directory"

Step 3: Creating the List of Files, Folders, and Subfolders

We'll utilize the os.walk() function to build the list. By traversing each subdirectory, this program creates the file names in a directory tree. The root directory, its subdirectories, and its files are the three values that are returned.

file_list = []
for root, dirs, files in os.walk(directory_path):
    for file in files:
        file_list.append(os.path.join(root, file))

In this code snippet, we iterate over each root directory, subdirectories, and files using the os.walk() function. For each file encountered, we append the absolute file path to the file_list using os.path.join() to concatenate the root and file names.

Step 4: Creating an Excel Spreadsheet

We can now develop an Excel spreadsheet to keep track of the files, folders, and subfolders that are present. For this, we'll employ the pandas library.

data = {"File Path": file_list}
df = pd.DataFrame(data)
df.to_excel("file_list.xlsx", index=False)

In this code snippet, we create dictionary data with a key "File Path" and the file_list as its corresponding value. We then create a DataFrame df using this dictionary. Finally, we use the to_excel() function to write the DataFrame to an Excel file named "file_list.xlsx". The index=False argument ensures that the index column is not included in the Excel file.

Step 5: Running the Script

Execute the Python script after saving it with extension. Ensure that the directory where the script is running has write permissions. The list of files, directories, and subfolders is contained in a file called "file_list.xlsx" that you can retrieve once the script has finished running.


In this article, we learned how to use Python and the os and pandas libraries to create a list of files, folders, and subfolders in Excel. This approach simplifies file structure organization and analysis, especially for large datasets. By customizing the script, you can include additional file metadata and leverage pandas functionalities for data manipulation. Ensure proper permissions when accessing files. Overall, this technique streamlines file management and offers a valuable tool for data exploration.

Updated on: 25-Jul-2023


Kickstart Your Career

Get certified by completing the course

Get Started