List All Files in Directory and Subdirectories in Python

List All Files in Directory and Subdirectories in Python

  1. Use os.walk() to List All Files in the Directory and Subdirectories in Python
  2. Use listdir() to List All Files in the Directory and Subdirectories in Python
  3. Use glob to List All Files in the Directory and Subdirectories in Python

Python provides many ways to access a list of files in a directory and subdirectories. This guide will walk through the os.walk(), listdir(), and glob.

Use os.walk() to List All Files in the Directory and Subdirectories in Python

The os module in Python provides a means to interact with the Operating System. It has many built-in functions that deal with the file system.

We can fetch, create, remove and change the directories using this module. The walk() is a recursive method that generates the file names in a directory tree by either walking in a top-down or bottom-up manner.

In the following code, we have displayed the files in the directory and the subdirectories of MyFolder. In our case, this folder is located in the home directory of Python’s environment.

Example Code:

import os
root = "MyFolder"
for path, subdirs, files in os.walk(root):
    for name in files:
        print(os.path.join(path, name))

Output:

MyFolder\text_file1.txt
MyFolder\text_file2.txt
MyFolder\SubFolder1\text_file3.txt
MyFolder\SubFolder1\text_file4.txt

Use listdir() to List All Files in the Directory and Subdirectories in Python

In the following code, we traverse the directory tree and access its files and sub-files without using the walk method. We have specified the root folder MyFolder as a starting point.

Then we accessed the directories and subdirectories of that folder using the listdir() and stored them in items. We have concatenated the item names with the previous path and checked if it is a directory using the isdir() method.

If so, we will further search that directory and apply the same procedure. We have appended the file name to the previous path if it is a file name and displayed them.

Example Code:

import os
root = "MyFolder"
files = []
walk = [root]
while walk:
    folder = walk.pop(0)+"/";
    items = os.listdir(folder) # items = folders + files
    for i in items:
        i=folder+i
        (walk if os.path.isdir(i) else files).append(i)
print(files)

Output:

['MyFolder/text_file1.txt', 'MyFolder/text_file2.txt', 'MyFolder/SubFolder1/text_file3.txt', 'MyFolder/SubFolder1/text_file4.txt']

Use glob to List All Files in the Directory and Subdirectories in Python

The glob is a built-in module in Python that stands for global. This module returns all file paths whose name and extension match a specific pattern.

Here we have used *.*, which indicates any file name having any extension. We use the glob.glob() to find files recursively inside a directory or a sub-directory.

The pattern ** will match any files and zero or more folders and subdirectories if recursive is set to True.

Example Code:

import glob
path = 'MyFolder\**\*.*'
for file in glob.glob(path, recursive=True):
    print(file)

Output:

MyFolder\text_file1.txt
MyFolder\text_file2.txt
MyFolder\SubFolder1\text_file3.txt
MyFolder\SubFolder1\text_file4.txt
Author: Fariba Laiq
Fariba Laiq avatar Fariba Laiq avatar

I am Fariba Laiq from Pakistan. An android app developer, technical content writer, and coding instructor. Writing has always been one of my passions. I love to learn, implement and convey my knowledge to others.

LinkedIn

Related Article - Python Directory

  • Python Get Home Directory
  • List Subdirectories of a Directory in Python
  • Fix the No Such File in Directory Error in Python
  • Get Directory From Path in Python
  • Execute a Command on Each File in a Folder in Python