Created
February 19, 2023 12:59
-
-
Save iKunalChhabra/9a0b740b6696e50bb6c6d10f74c16f75 to your computer and use it in GitHub Desktop.
Convert bytes to human readable unit and 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 convert_bytes(num): | |
for x in ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']: | |
if num < 1024.0: | |
return f"{num:.2f} {x}" | |
num /= 1024.0 | |
print(convert_bytes(1000_000_000_000)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment