""" Adpated from: https://stackoverflow.com/a/14822210/4189676 """ from math import floor, log def bytes_to_human_readable(number_of_bytes: int) -> str: magnitude: int = int(floor(log(number_of_bytes, 1024))) value: float = number_of_bytes / pow(1024, magnitude) if magnitude > 3: return f'{value:.1f} TiB' return '{:3.2f} {}B'.format(value, ('', 'Ki', 'Mi', 'Gi')[magnitude])