def human_filesize(nbytes): suffixes = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'] i = 0 while nbytes >= 1024 and i < len(suffixes) - 1: nbytes /= 1024. i += 1 f = ('%.2f' % nbytes).rstrip('0').rstrip('.') return '%s %s' % (f, suffixes[i])