Convert bytes to human readable, in Python

def human_readable(size, suffix='B'):
    fmt = '{:3.1f}{}{}'
    units = ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']
    for unit in units:
        if abs(size) < 1024.0:
            return fmt.format(size, unit, suffix)
        size /= 1024.0
    return fmt.format(size, 'Yi', suffix)

Created: 2024-01-29
Last Updated: 2024-01-29

← Back