Bring value from walk()

Gerrit Holl gerrit.holl at pobox.com
Fri Feb 25 13:50:51 CET 2000


<quote name="Milos Prudek" date="951477167">
> I need a function that walks the dir tree and adds up occupied space.

class DiskUsage:
    __size = 0

    def adddir(self, dir):
        files = os.listdir(dir)
        dirs = []
        for file in files:
            filename = os.path.join(dir, file)
            s = os.lstat(filename)
            mode = s[ST_MODE]
            if S_ISDIR(mode):
                dirs.append(filename)
            elif S_ISREG(mode):
                self.__size = self.__size + s[ST_SIZE]
        for dir in dirs:
            self.adddir(dir)

    def len(self):
        return self.__size

def du(dir):
    '''du(dir) -> int

    Returns the size in bytes of 'dir'.
    '''

    disk = DiskUsage()
    disk.adddir(dir)
    return disk.len()


-- 
Comparison Python GUI's: http://www.nl.linux.org/~gerrit/gui.html
Please comment!




More information about the Python-list mailing list