Python - How to list all files in a directory and its sub-directories

By xngo on June 7, 2019

Overview

In Python, os.walk() method allows you to list all files of a directory, including files from its sub-directories. It generates the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory it walked through, it returns 3-tuple: dirpath, dirnames and filenames.

  • dirpath: It is the path to the directory.
  • dirnames: It is a list of the names of the subdirectories in dirpath.
  • filenames: It is a list of the filenames in dirpath.

List all files

The code below shows how to list all files from the test directory, including files from its subdirectories.

import os
 
target_dir = "test"
 
# Get the full path name of files from test/ directory  and its sub-directories.
for (dirpath, dirname, filename) in os.walk(target_dir):
    for file in filename:
        print(os.path.join(dirpath, file))

Output

An example of output from my test directory.

test/file-1.txt
test/subdir-level-1/music-a.mp3
test/subdir-level-1/file-2.txt
test/subdir-level-1/subdir-level-2/music-b.mp3
test/subdir-level-1/subdir-level-2/file-3.txt

Filter certain files

We can also add condition to only list text files. Here the example.

import os
 
target_dir = "test"
 
# List all files ending with *.txt.
for (dirpath, dirname, filename) in os.walk(target_dir):
    for file in filename:
        if ".txt" in file:
            print(os.path.join(dirpath, file))

Output

An example of output from my test directory.

test/file-1.txt
test/subdir-level-1/file-2.txt
test/subdir-level-1/subdir-level-2/file-3.txt

List only directories

import os
 
target_dir = "test/"
 
# List all directories.
for (dirpath, dirname, filename) in os.walk(target_dir):
    print(dirpath)

Output

An example of output from my test directory.

test/
test/subdir-level-1
test/subdir-level-1/subdir-level-2

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.