-
-
Save zeandrade/6489a86c7c8959019fc6fdf060f4cde1 to your computer and use it in GitHub Desktop.
Python: Human readable file size
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
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