Created
July 25, 2021 08:25
-
-
Save dayyass/78d99343231196c2114036f760cb20dc to your computer and use it in GitHub Desktop.
Convert bytes to 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 humanize_bytes(bytes: int, suffix: str = "B") -> str: | |
""" | |
Convert bytes to human readable format. | |
:param int bytes: number of bytes. | |
:param str suffix: bytes suffix. | |
:return: human readable size. | |
:rtype: str | |
""" | |
for unit in ["", "K", "M", "G", "T", "P"]: | |
if bytes >= 1024: | |
bytes /= 1024 | |
else: | |
break | |
return f"{bytes:.2f}{unit}{suffix}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment