Count All Directories in Given Directory and Subdirectories using Python

os.walk function

import os

rootDir = 'test_dir'

numberOfDirs = 0

for root, dirs, files in os.walk(rootDir):
    numberOfDirs += len(dirs)

print(numberOfDirs)

Leave a Comment

Cancel reply

Your email address will not be published.