Size of a File Folder/Directory (Python)

vegaseat

This handy little utility determines the size of a folder and it's subfolders in MBytes.

11,850 Views
About the Author

Scientist

# determine size of a given folder in MBytes

import os

# pick a folder you have ...
folder = 'D:\\zz1'
folder_size = 0
for (path, dirs, files) in os.walk(folder):
  for file in files:
    filename = os.path.join(path, file)
    folder_size += os.path.getsize(filename)

print "Folder = %0.1f MB" % (folder_size/(1024*1024.0))
nirav200_patel 0 Newbie Poster

# determine size of a given folder in MBytes import os # pick a folder you have ...folder = 'D:\\zz1'folder_size = 0for (path, dirs, files) in os.walk(folder): for file in files: filename = os.path.join(path, file) folder_size += os.path.getsize(filename) print "Folder = %0.1f MB" % (folder_size/(1024*1024.0))

ilovepython1978 0 Newbie Poster

Very good!

l77 0 Newbie Poster

Is this really recursing through subfolders? I don't think so... it's only counting the root folder's files size, flat.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Why don't you test it first before you make an assumption?

Arun Mittal 0 Newbie Poster

though it is working its not giving the actual size. calulated size is always less than actual size

kostas89 0 Newbie Poster

yes sure, but 45 Mbytes difference isnt possible. Anyway thank y that y shared with us this code but better remove or modifty it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.