QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Convert Bytes to Human-Readable Format

Converts a size in bytes to a human-readable format.

  • bytes
  • format

Code

def bytes_to_human_readable(num):
    for unit in ['B', 'KB', 'MB', 'GB', 'TB', 'PB']:
        if num < 1024:
            return f"{num:.2f} {unit}"
        num /= 1024

Usage

bytes_to_human_readable(123456789) # Returns: '117.74 MB'