# 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))
6
Contributors
6
Replies
21
Views
10 Years
Discussion Span
Last Post by kostas89
0

# 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))

0

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

0

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

0

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

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.