-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_start.py
More file actions
28 lines (21 loc) · 908 Bytes
/
python_start.py
File metadata and controls
28 lines (21 loc) · 908 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
SUFFIXES = {1000: ['KB','MB','GB','TB','PB','EB','ZB','YB'],
1024: ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB']}
def approximate_size(size,a_kilobyte_is_1024_bytes=True):
'''Conver a file size to human-readable form
keyword arguments:
size -- file size in byes
a_kilobyte_is_1024_bytes -- if True(default),use multiples of 1024
if False, use multiples of 1000
Returns: string
'''
if size < 0:
raise ValueError('number must be non-negative')
multiple = 1024 if a_kilobyte_is_1024_bytes else 1000
for suffix in SUFFIXES[multiple]:
size /= multiple
if size < multiple:
return '{0:.1f} {1}'.format(size,suffix)
raise ValueError('number too large')
if __name__ == "__main__":
print(approximate_size(1000000000000, False))
print(approximate_size(1000000000000))