How to traverse all folders and files within a folder dynamically in Python 3

I had this requirement where I need to be able to look into a folder and pick up any configuration files for my Python 3 application. In order to achieve that, I first set an exploratory task to get the Python 3 code for traversing all folders and files within a folder.

For this exploratory task, I had created a script that will traverse the folders and files that are contained within a folder and print out their full paths.

Python 3 code for traversing all folders and files within a folder dynamically, from bottom to top

import os

def traverseDir(folderPath):

    for subFolderRoot, foldersWithinSubFolder, files in os.walk(folderPath, topdown=False):

        for fileName in files:
            print(os.path.join(subFolderRoot, fileName))

        for folderNameWithinSubFolder in foldersWithinSubFolder:
            print(os.path.join(subFolderRoot, folderNameWithinSubFolder))


traverseDir('/sample-dir')

In the above script, I defined a function traverseDir which will take in the path of the folder which the caller wishes to traverse via the folderPath variable.

It then utilises the os.walk function to traverse the contents of the folder specified by folderPath. By using topdown=False, we tell os.walk to traverse the folder tree depth-first.

Each iteration of os.walk returns us the path to a subfolder, a list of folders and a list of files within that subfolder. With that, I then:

  1. loop the list of files and print their full path.
  2. loop the list of folders and print their full path.

Sample use case and output

A sample file hierarchy

In order to test whether the Python 3 script is able to traverse all folders and files within a folder dynamically, I created some files and folders with the following hireachical structure:

/sample-dir
--fileA.txt
--folderA
----fileB.txt
----folderC
------fileC.txt
--folderB
----fileD.txt
----folderD

Output from running the Python 3 script (with topdown=False supplied to os.walk)

Running the Python 3 script that I had created on /sample-dir, I would yield the following result:

/sample-dir/folderA/folderC/fileC.txt
/sample-dir/folderA/fileB.txt
/sample-dir/folderA/folderC
/sample-dir/folderB/fileD.txt
/sample-dir/folderB/folderD
/sample-dir/fileA.txt
/sample-dir/folderA
/sample-dir/folderB

Output from running the Python 3 script (with topdown=True supplied to os.walk)

Running the Python 3 script on /sample-dir with topdown=True I would yield the following result:

/sample-dir/fileA.txt
/sample-dir/folderA
/sample-dir/folderB
/sample-dir/folderA/fileB.txt
/sample-dir/folderA/folderC
/sample-dir/folderA/folderC/fileC.txt
/sample-dir/folderB/fileD.txt
/sample-dir/folderB/folderD

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.