Skip to content

Instantly share code, notes, and snippets.

@VerosK
Forked from danlmyers/humanbytes.py
Created August 23, 2017 20:21
Show Gist options
  • Save VerosK/85aa7c83e8ad826549c05e208778757e to your computer and use it in GitHub Desktop.
Save VerosK/85aa7c83e8ad826549c05e208778757e to your computer and use it in GitHub Desktop.
python humanize bytes
#Humanize byte size figures
def humanize_bytes(bytesize, precision=2):
abbrevs = (
(1<<50, 'PB'),
(1<<40, 'TB'),
(1<<30, 'GB'),
(1<<20, 'MB'),
(1<<10, 'kB'),
(1, 'bytes')
)
if bytesize == 1:
return '1 byte'
for factor, suffix in abbrevs:
if bytesize >= factor:
break
return '%.*f %s' % (precision, bytesize / factor, suffix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment