python calculator file size to megabytes

Solutions on MaxInterview for python calculator file size to megabytes by the best coders in the world

showing results for - "python calculator file size to megabytes"
Anna
09 Jul 2019
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])