0

I work with one big directory (Project) that divided into 3 subfolders (a, lyr, out) and they divided into sub-subfolders with a lot of files inside them:

enter image description here

enter image description here

I try to get the size of those 3 sub folders- each subfolder separately. When i using this code (i red Calculating a directory size using Python?):

 import os

 def get_size(start_path = "."):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(start_path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            total_size += os.path.getsize(fp)
    return total_size
path = r"G:\desktop\Project"
dirList = os.listdir(path)
for fname in dirList:
    print fname
    print get_size(path)

i get:

>>> 
a
41730716
lyr
41730716
out
41730716
>>> 

I don't understand what is my mistake.

41730716 is the size of all "Project" directory, and this is not what i want. I need the size of subfolders: a, lyr, out each. Actually, the size of subfolder a is 23.4 MB ,lyr is 12 MB ,out is 4 MB- those values i need to get in the result.

I red: in Calculating size folder python. in this question, i got only the directory size including all subfolders size- that isn't what i need.

newGIS
  • 598
  • 3
  • 10
  • 26

1 Answers1

0

finally, i used this code:

import os
def get_size(start_path = "."):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(start_path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            total_size += os.path.getsize(fp)
    return "Folder = %0.1f MB" % (total_size/(1024*1024.0))
os.chdir(r"G:\desktop\Project")
all_subdirs = [ d for d in os.listdir('.') if os.path.isdir(d)]
for dirs in all_subdirs:
    dir = os.path.join(r"G:\desktop\Project", dirs)
    os.chdir(dir)
    current = os.getcwd()
    print current
    print get_size(current)
newGIS
  • 598
  • 3
  • 10
  • 26