Skip to content

Instantly share code, notes, and snippets.

@flyhigher139
Created May 5, 2019 05:49
Show Gist options
  • Save flyhigher139/0f591890d0f2e2113f349e349f29b0aa to your computer and use it in GitHub Desktop.
Save flyhigher139/0f591890d0f2e2113f349e349f29b0aa to your computer and use it in GitHub Desktop.
Get human readable file size
"""
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