def sizeof_fmt(num):
    """
    Returns a number of bytes in a more human-readable form.
    Scaled to the nearest unit that there are less than 1024 of, up to
    a maximum of TBs.
    Thanks to stackoverflow.com/questions/1094841.
    """
    for unit in ['bytes', 'KB', 'MB', 'GB']:
        if num < 1024.0:
            return "%3.1f%s" % (num, unit)
        num /= 1024.0
    return "%3.1f%s" % (num, 'TB')