def size_fmt(num, decimals = 1):
    """
    Formats file sizes in human readable format. Anything bigger than TB
    is returned is TB. Number of decimals is optional, defaults to 1.
    """
    fmt = "%%3.%sf %%s"% decimals
    for x in ['bytes','KB','MB','GB']:
        if num < 1024.0:
            return fmt % (num, x)
        
        num /= 1024.0
        
    return fmt % (num, 'TB')