Skip to content

Instantly share code, notes, and snippets.

@britonad
Created August 17, 2017 14:49
Show Gist options
  • Save britonad/7188abf8d822e71cd8aca59746c15cdd to your computer and use it in GitHub Desktop.
Save britonad/7188abf8d822e71cd8aca59746c15cdd to your computer and use it in GitHub Desktop.
def convert_size(size_bytes):
if size_bytes == 0:
return "0B"
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size_bytes, 1024)))
p = math.pow(1024, i)
s = round(size_bytes / p, 2)
return "%s %s" % (s, size_name[i])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment