Calculating directory sizes in Python

A very simple way of getting the total size of a certain directory.

All it does is use Python os.path.walk and keep track of the accumulated size while walking the directory tree.

import os
def getDirectorySize(directory):
    class TotalSize:
        def __init__(self):
            self.total = 0

    def visit(totalSize, dirname, names):
        for name in names:
            absFilename = os.path.join(dirname, name)
            if os.path.isfile(absFilename):
                totalSize.total += os.path.getsize(absFilename)

    totalSize = TotalSize()
    os.path.walk(directory, visit, totalSize)
    return totalSize.total

Example usage:

print getDirectorySize('/home/rs')

And the output is:

91904975510

Which means my home directory is about 87 GB! One thing to note is that it will double count if you have multiple symlinks that point to the same file in the same tree.

Tags: ,

2 Responses to “Calculating directory sizes in Python”

  1. The isfile() check is just what I needed to get a script working (here if interested: http://dancingpenguinsoflight.com/2009/01/tracking-down-used-disk-space-follow-largest-directories; it follows the largest dir in each dir it enters). Thanks!

  2. rs says:

    @Samuel – Nice script you’ve got there :) Useful when tracking down why /opt is down to only 1% free space available.

Leave a Reply