python get file size in mb

Solutions on MaxInterview for python get file size in mb by the best coders in the world

showing results for - "python get file size in mb"
Aisling
06 Jul 2017
1import os
2os.path.getsize("path/to/file") / (1024*1024)
Aaron
14 Nov 2016
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])