Skip to content

Instantly share code, notes, and snippets.

@ktmud
Forked from cbwar/size.py
Created March 27, 2018 20:50
Show Gist options
  • Save ktmud/72dadd142bd44b29931c1c270a11a9c8 to your computer and use it in GitHub Desktop.
Save ktmud/72dadd142bd44b29931c1c270a11a9c8 to your computer and use it in GitHub Desktop.
Python: Human readable file size
def sizeof_fmt(num, suffix='o'):
"""Readable file size
:param num: Bytes value
:type num: int
:param suffix: Unit suffix (optionnal) default = o
:type suffix: str
:rtype: str
"""
for unit in ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z']:
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