-
-
Save VerosK/85aa7c83e8ad826549c05e208778757e to your computer and use it in GitHub Desktop.
python humanize bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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