Last active
April 29, 2024 20:08
-
-
Save ElectroDrome/bf86b69a9a0dc4a3d17e968256f99270 to your computer and use it in GitHub Desktop.
Python: Format a number of bytes into a human readable format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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