10 examples of 'python get file size in mb' in Python

Every line of 'python get file size in mb' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
21def size_in_mb(file_fn):
22 return os.path.getsize(file_fn)/(1024**2)
247def get_size(file):
248 """Gets the size of the file."""
249 check_exists(file)
250 size = os.stat(file).st_size
251 return size
20@property
21def mb(self):
22 return self._bytes / 1024.0 / 1024.0
64def get_filesize(self, file_path):
65 cmd = "ls -l %s | awk '{print $5}'" % file_path
66 res = commands.getoutput(cmd)
67 return(res)
105def sizeInStr(path):
106 if os.path.exists(path):
107 b = os.path.getsize(path)
108 kb = round(b / 1024, 10)
109 mb = round(kb / 1024, 10)
110 gb = round(mb / 1024, 10)
111
112 if gb >= 1:
113 return str(round(gb, 2)) + " GiB"
114 elif mb >= 1:
115 return str(round(mb, 2)) + " MiB"
116 elif kb >= 1:
117 return str(round(kb, 2)) + " KiB"
118 else:
119 return str(b) + " B"
120 else:
121 return "X GiB"
129def filesize(bytes_: float) -> str:
130 """Render a file size in bytes
131
132 Example:
133 >>> filesize(12345678)
134 "12,345,678"
135 """
136 val_str = "%.0f" % float(bytes_)
137 if len(val_str) <= 3:
138 return "%s B" % val_str
139
140 offset = len(val_str) % 3
141 groups = [val_str[0:offset]] + [val_str[i:i + 3] for i in range(offset, len(val_str), 3)]
142 return "%s B" % ','.join(groups)
242def get_size(path):
243 total = 0
244
245 # CSV files are broken up by dask when they're written out
246 if os.path.isfile(path):
247 return os.path.getsize(path)
248 elif path.endswith('csv'):
249 for csv_fpath in glob.glob(path.replace('.csv', '*.csv')):
250 total += os.path.getsize(csv_fpath)
251 return total
252
253 # If path is a directory (such as parquet), sum all files in directory
254 for dirpath, dirnames, filenames in os.walk(path):
255 for f in filenames:
256 fp = os.path.join(dirpath, f)
257 total += os.path.getsize(fp)
258
259 return total
86def FileSizeAsString(file_path):
87 """Returns size of file as a string."""
88 return SizeAsString(FileSize(file_path))
118@staticmethod
119def get_total_size(files):
120 totalSize = 0
121 for f in files:
122 totalSize += f['bytes']
123 return totalSize
34def human_size(size_bytes):
35 """
36 format a size in bytes into a 'human' file size, e.g. bytes, KB, MB, GB, TB, PB
37 Note that bytes/KB will be reported in whole numbers but MB and above will have greater precision
38 e.g. 1 byte, 43 bytes, 443 KB, 4.3 MB, 4.43 GB, etc
39 """
40 if size_bytes == 1:
41 # because I really hate unnecessary plurals
42 return "1 byte"
43
44 suffixes_table = [('bytes',0),('KB',0),('MB',1),('GB',2),('TB',2), ('PB',2)]
45
46 num = float(size_bytes)
47 for suffix, precision in suffixes_table:
48 if num < 1024.0:
49 break
50 num /= 1024.0
51
52 if precision == 0:
53 formatted_size = "%d" % num
54 else:
55 formatted_size = str(round(num, ndigits=precision))
56
57 return "%s %s" % (formatted_size, suffix)

Related snippets