0

How to calculate size of immediate subfolders of a folder using os.walk()

Let say, I have a directory

Directory-SubDirectory1
         -SubDirectory2
         -SubDirectory3
         -SubDirectory4

So, I want to calculate size of all subdirectories individually, like:

SubDirectory1 Size 100 MB
SubDirectory2 Size 110 MB
etc

I tried-

for r, d, f in os.walk('/dbfs/mnt/Directory/.../'):
    size = sum(getsize(join(r,n)) for n in f) / 1048576
    print(size)
    for s in d:
        print (s)

Which returns the sizes of all subfolders and files in a long loop.

I want just immediate folder size result.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Crime_Master_GoGo
  • 1,641
  • 1
  • 20
  • 30
  • 1
    Does this answer your question? [Calculating a directory's size using Python?](https://stackoverflow.com/questions/1392413/calculating-a-directorys-size-using-python) – Andriy Ivaneyko Jun 05 '20 at 12:04

2 Answers2

0

Using pathlib you can avoid a lot of OS specific headaches. It's also a bit more compact, use timeit to compare if you'd like, but I have a feeling it's quite a bit faster too.

from pathlib import Path

def dirsize(root):
    size = 0
    for f in Path(root).iterdir():
        size += f.stat().st_size / 1048576
        if f.is_dir():
            print(f, "size: ", f.stat().st_size / 1048576)
    return size

Try running with dirsize("/dbfs/mnt/Directory/...")

Here is an example, you can clean up the printing:

>>> dirsize("E:\\Documents")
E:\Documents\knowledge size:  0.01171875
E:\Documents\Piano Scores size:  0.0078125
E:\Documents\poetry size:  0.00390625
E:\Documents\programming size:  0.00390625
E:\Documents\projects size:  0.0
E:\Documents\theory size:  0.00390625
16800.917387008667```
GRAYgoose124
  • 621
  • 5
  • 24
0

I made this finally and works fine-

import os
from pathlib import Path
root='/dbfs/mnt/datalake/.../'
size = 0
for path, subdirs, files in os.walk(root):
  for f in Path(root).iterdir():
    if name in files:
      if f.is_dir():
        size += os.path.getsize(os.path.join(path, name))
        dirSize = size/(1048576)
        print(f, "--Size:", dirSize)
Crime_Master_GoGo
  • 1,641
  • 1
  • 20
  • 30