Created
December 4, 2017 10:10
-
-
Save Ricky-Wilson/0d608bb7a3ed0cfa5318b444bad594a9 to your computer and use it in GitHub Desktop.
Convert file sizes to human readable format
This file contains hidden or 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 convert_size(size_bytes): | |
if size_bytes == 0: | |
return "0B" | |
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") | |
i = int(math.floor(math.log(size_bytes, 1024))) | |
power = math.pow(1024, i) | |
size = round(size_bytes / power, 2) | |
return "%s %s" % (size, size_name[i]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment