Skip to content

Instantly share code, notes, and snippets.

@kebyn
Created February 26, 2021 05:57
Show Gist options
  • Save kebyn/087dc4dbddddce5094ae52b98fc13a75 to your computer and use it in GitHub Desktop.
Save kebyn/087dc4dbddddce5094ae52b98fc13a75 to your computer and use it in GitHub Desktop.
def bytes2human(in_bytes: int) -> str:
'''
'''
suffixes = ('B', 'KB', 'MB', 'GB', 'TB', 'PB')
suffix = 0
while in_bytes > 9999 and suffix < len(suffixes):
in_bytes = in_bytes >> 10
suffix += 1
if suffix >= len(suffixes):
suffix -= 1
return "{}{}".format(in_bytes, suffixes[suffix])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment