Skip to content

Instantly share code, notes, and snippets.

@cizixs
Created May 21, 2016 04:03
Show Gist options
  • Save cizixs/be41bbede49a772791c08491801c396f to your computer and use it in GitHub Desktop.
Save cizixs/be41bbede49a772791c08491801c396f to your computer and use it in GitHub Desktop.
Convert file/memory size of int number to human readable format.
"""
From: http://stackoverflow.com/a/1094933/1925083
Supports:
all currently known binary prefixes
negative and positive numbers
numbers larger than 1000 Yobibytes
arbitrary units (maybe you like to count in Gibibits!)
Example:
>>> sizeof_fmt(168963795964)
'157.4GiB'
"""
def sizeof_fmt(num, suffix='B'):
for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
if abs(num) < 1024.0:
return "%3.1f%s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f%s%s" % (num, 'Yi', suffix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment