How to count size of a directory using os.walk() python?

Viewed 300

How to count size of a directory using os.walk() python?

I could get all the root files using below code-

root = "/dbfs/mnt/datalake/Persistent/External/"
for path, subdirs, files in os.walk(root):
    for name in files:
        print (os.path.join(path, name))

But I need total directory size of "External". How can I achieve this?

1 Answers

Like this?

import os
size=0
root = "/dbfs/mnt/datalake/Persistent/External/"
for path, subdirs, files in os.walk(root):
    for name in files:
        size+=os.path.getsize(os.path.join(path, name))

print(size)
Related