Simple py script to calc folder sizes

Caleb Hattingh caleb.hattingh at
Wed Mar 22 00:19:39 CET 2006

Hi everyone

[Short version: I put a some code below: what changes can make it run

Unless you have a nice tool handy, calculating many folder sizes for
clearing disk space can be a click-fest nightmare.   Looking around, I
found Baobab (gui tool); the "du" linux/unix command-line tool; the
extremely impressive tkdu: ; a python
script I didn't really understand at (are these "folder
objects" zope thingies?);  there are also tools that can add a
"foldersize" column into Explorer on Windows
(, for example);  the superb freeCommander
file-manager (win32) has the functionality built in, and so on.

"du" is closest to what I was looking for, but is not immediately
cross-platform: I know I can probably get it through Cygwin, and there
is probably a win32 binary or clone around somewhere, but I thought a
simple python solution would be great.  Maybe there already is one, but
I couldn't find it with a modest amount of searching.

Anyway, I made one that will produce a list of only the folders in the
current folder, along with their sizes.  I am posting it for two
reasons: it might be useful for someone else, and I want to know if it
can be made faster (but in a cross-platform way); maybe you spot
something in the code that is obviously sub-optimal.

# Python script to list sizes of folders in current folder

import os, os.path

rootfolders = os.listdir('.')
rootfolders = [i for i in rootfolders if os.path.isdir(i)]

class counter:
    def __init__(self,rootfolder):
        self.count = 0
        self.rootfolder = rootfolder
    def inc(self,num):
        self.count = self.count + num
    def __str__(self):
        if self.count<1024.:
            unit = ' bytes'
            scaler = 1.
        elif self.count<1024.*1024.:
            unit = ' KB'
            scaler = 1/1024.
        elif self.count<1024.*1024.*1024.:
            unit = '   MB'
            scaler = 1/1024./1024.
            unit = '     GB'
            scaler = 1/1024./1024./1024.
        return '%-20s -

def visitfun(cntObj,dirname,names):
    for i in names:
        fullname = os.path.join(dirname,i)
        if os.path.isfile(fullname):
   os.path.getsize(fullname) )
    return None

foldersizeobjects = []
for i in rootfolders:
    cntObj = counter(i)

def cmpfunc(a,b):
    if a.count > b.count:
        return 1
    elif a.count == b.count:
        return 0
        return -1


for foldersize in foldersizeobjects:
    print foldersize
print 'Total: %.2f MB'%(tot/1024./1024.)

# End


More information about the Python-list mailing list