Count All Files in Given Directory and Subdirectories using Python

os.walk function

import os

rootDir = 'test_dir'

numberOfFiles = 0

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

print(numberOfFiles)

Leave a Comment

Cancel reply

Your email address will not be published.