1import math
2
3def convert_size(size_bytes):
4 if size_bytes == 0:
5 return "0B"
6 size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
7 i = int(math.floor(math.log(size_bytes, 1024)))
8 p = math.pow(1024, i)
9 s = round(size_bytes / p, 2)
10 return "%s %s" % (s, size_name[i])