Skip to content

Instantly share code, notes, and snippets.

@wohali
Created April 19, 2017 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wohali/415d3303f79e82130d0a3878cc40c1a9 to your computer and use it in GitHub Desktop.
Save wohali/415d3303f79e82130d0a3878cc40c1a9 to your computer and use it in GitHub Desktop.
def _size_format(bytes, precision=2):
"""Returns a humanized string for a given amount of bytes"""
bytes = int(bytes)
if bytes < 1000:
return "{} bytes".format(bytes)
log = math.floor(math.log(bytes, 1000))
return "{:.{}f} {}".format(
bytes / math.pow(1000, log),
precision,
['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'][int(log)]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment