Skip to content

Instantly share code, notes, and snippets.

@j-min
Created January 6, 2017 16:14
Show Gist options
  • Save j-min/54133f344489a942bcb4a6e135ceae30 to your computer and use it in GitHub Desktop.
Save j-min/54133f344489a942bcb4a6e135ceae30 to your computer and use it in GitHub Desktop.
convertSize.py
import math
def convertSize(size):
"""
Return filesize (in Bytes) in human-readable format
"""
if (size == 0):
return '0B'
units = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size,1024)))
p = math.pow(1024,i)
size = round(size/p,2)
return '{}{}'.format(size, units[i])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment