Created
January 6, 2017 16:14
-
-
Save j-min/54133f344489a942bcb4a6e135ceae30 to your computer and use it in GitHub Desktop.
convertSize.py
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
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