Created
May 21, 2016 04:03
-
-
Save cizixs/be41bbede49a772791c08491801c396f to your computer and use it in GitHub Desktop.
Convert file/memory size of int number to human readable format.
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
""" | |
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