Created
May 5, 2019 05:49
-
-
Save flyhigher139/0f591890d0f2e2113f349e349f29b0aa to your computer and use it in GitHub Desktop.
Get human readable file size
This file contains hidden or 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
""" | |
https://stackoverflow.com/questions/1094841/reusable-library-to-get-human-readable-version-of-file-size | |
>>> 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