Skip to content

Instantly share code, notes, and snippets.

@lsbardel
Created February 9, 2012 09:44
Show Gist options
  • Save lsbardel/1778912 to your computer and use it in GitHub Desktop.
Save lsbardel/1778912 to your computer and use it in GitHub Desktop.
Convert a number of bytes into a human readable memory usage
memory_symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
memory_size = dict(((s,1 << (i+1)*10) for i,s in enumerate(memory_symbols)))
def convert_bytes(b):
'''Convert a number of bytes into a human readable memory usage'''
if b is None:
return '#NA'
for s in reversed(memory_symbols):
if b >= memory_size[s]:
value = float(b) / memory_size[s]
return '%.1f%sB' % (value, s)
return "%sB" % b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment