Skip to content

Instantly share code, notes, and snippets.

@zeandrade
Forked from cbwar/size.py
Last active July 4, 2018 15:14
Show Gist options
  • Save zeandrade/6489a86c7c8959019fc6fdf060f4cde1 to your computer and use it in GitHub Desktop.
Save zeandrade/6489a86c7c8959019fc6fdf060f4cde1 to your computer and use it in GitHub Desktop.
Python: Human readable file size
def sizeof_fmt(num, iec=True):
"""Readable file size
:param num: Bytes value
:type num: int
:param iec: default True, set iec pattern (powers of 1024),
False set si pattern (powers of 1000)
:type iec: boolean
:rtype: str
"""
ext_list = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']
divisor = 1000.0
if iec:
ext_list = [item + 'i' for item in ext_list]
divisor += 24.0
for unit in ext_list:
if abs(num) < divisor:
break
num /= divisor
return "%.1f %sB" % (num, unit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment