Skip to content

Instantly share code, notes, and snippets.

@iKunalChhabra
Created February 19, 2023 12:59
Show Gist options
  • Save iKunalChhabra/9a0b740b6696e50bb6c6d10f74c16f75 to your computer and use it in GitHub Desktop.
Save iKunalChhabra/9a0b740b6696e50bb6c6d10f74c16f75 to your computer and use it in GitHub Desktop.
Convert bytes to human readable unit and format
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