4 Ways To List All Subdirectories in a Directory – Python

Do you want to list all the subdirectories present in a directory in Python? One day I was trying to do the same but had no idea about it, then I went through forums and python tutorials and came up with the below solutions.

You can use the OS library present in Python to get the methods such as listdir(), scandir(), walk(), or glob. Using any of these four methods you can easily list all the subdirectories present in the directory.

Using os.walk() Method to List All The Subdirectories in a Directory

The os.walk() method is more intuitive and it returns a tuple of root directories, subdirectories, and filenames. Using os.walk() method is the easiest way to get the list of all the subdirectories present in the current and all subdirectories as well.

#importing OS library
import os

#Intializing the Path of Directory
pathOfDirectory = "C:\Python310\\"

#Creating Empty List of subDirectories
listOfSub = []

#getting All Entries Present in the Given Directory
for rootDir, subDirectory, Files in os.walk(pathOfDirectory):
    if os.path.isdir(rootDir):
        listOfSub.append(rootDir)
print(listOfSub)

Output:

This will print all the subdirectories till the end of the root of all other subdirectories.

If you want to only list the subdirectory in the current folder and not the subdirectories of the subdirectory then you need to modify the above code slightly as below.

#importing OS library
import os

#Intializing the Path of Directory
pathOfDirectory = "C:\Python310\\"

#Creating Empty List of subDirectories
listOfSub = next(os.walk(pathOfDirectory))[1]

print(listOfSub)

Output:

['DLLs', 'Doc', 'include', 'Lib', 'libs', 'Scripts', 'tcl', 'Tools']

Using listdir() to List All Subdirectories in a Directory

The listdir method is present in the OS library of Python that can be used to get the list of all the entries in a directory given as input by Path. Note that the list returning using this function is in arbitrary order.

Hence this method takes Path as an input argument and returns the list of entries present in the directory.

Let us see in the below example code the usage of the listdir() method in Python.

#importing OS library
import os

#Intializing the Path of Directory
pathOfDirectory = "C:\Python310\\"

#getting All Entries Present in the Given Directory
listOfAllEntries = os.listdir(pathOfDirectory)

#Creating an Empty list of Sub Directory
listOfSubDir = []

for item in listOfAllEntries:
    if os.path.isdir(os.path.join(pathOfDirectory, item)):
        listOfSubDir.append(item)

print(listOfSubDir)

Output:

['DLLs', 'Doc', 'include', 'Lib', 'libs', 'Scripts', 'tcl', 'Tools']

The listdir method will list out all the entries in the directory and hence we need to filter only subdirectory names from the list and this can be achieved by using path.isdir() method. It will return true if the input Path is Directory else it will return false.

Using os.scandir() Method to List All Subdirectories in a Directory

You can also use the os.scandir() to list all the subdirectories. This method is the fastest of all the methods discussed here. It is recommended to use the scandir method if you want to get the list of subdirectories quickly if you are having huge subdirectories to scan.

#importing OS library
import os

#Intializing the Path of Directory
pathOfDirectory = "C:\Python310\\"

#Creating List of subDirectories using os.scandir
subDirectories = [ subDir.path for subDir in os.scandir(pathOfDirectory) if subDir.is_dir() ]

print(subDirectories)

Output:

['C:\\Python310\\DLLs', 'C:\\Python310\\Doc', 'C:\\Python310\\include', 'C:\\Python310\\Lib', 'C:\\Python310\\libs', 'C:\\Python310\\Scripts', 'C:\\Python310\\tcl', 'C:\\Python310\\Tools']

Using glob Method

You can also use the glob Method to list all the subdirectories in a Directory. Below is the code example to follow.

#importing glob library
from glob import glob

#Intializing the Path of Directory
pathOfDirectory = "C:/Python310/"

#Creating List of subDirectories using os.scandir
subDirectories = glob(pathOfDirectory+"*/")

#Printing the List of Subdirectories
print(subDirectories)

Output:

['C:/Python310\\DLLs\\', 'C:/Python310\\Doc\\', 'C:/Python310\\include\\', 'C:/Python310\\Lib\\', 'C:/Python310\\libs\\', 'C:/Python310\\Scripts\\', 'C:/Python310\\tcl\\', 'C:/Python310\\Tools\\']
4 Ways To List All Subdirectories in a Directory - Python

Wrap Up

I hope you got the answer to the problem of how to list all the subdirectories in a directory using Python. From all the above methods listed above the most recommended and faster method os.scandir and the least efficient is os.walk.

Hence make sure which one are you using based on your requirement. Let me know if you have any other efficient method that is not listed here I will be happy to add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Dict To list – Convert A Dictionary to a List in Python
  2. Discord.py Bot Add Reaction To a Message Python
  3. Python Get The Minimum Value In Dictionary with Key
  4. How To Append Multiple Values To A List in Python
  5. How To Reboot or Restart Python Script
  6. Python: To Check If String Contains a Substring

Leave a Comment