Skip to content

Instantly share code, notes, and snippets.

@ElectroDrome
Last active April 29, 2024 20:08
Show Gist options
  • Save ElectroDrome/bf86b69a9a0dc4a3d17e968256f99270 to your computer and use it in GitHub Desktop.
Save ElectroDrome/bf86b69a9a0dc4a3d17e968256f99270 to your computer and use it in GitHub Desktop.
Python: Format a number of bytes into a human readable format
def format_bytes(num, suffix="B"):
for unit in ("", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"):
if abs(num) < 1024.0:
return f"{num:3.1f}{unit}{suffix}"
num /= 1024.0
return f"{num:.1f}Yi{suffix}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment