Adjusted convert_to_bytes
* Now supports bytes! * Now supports petabytes?
This commit is contained in:
parent
4228c9a3a9
commit
496b28c8a6
1 changed files with 13 additions and 9 deletions
|
|
@ -173,18 +173,22 @@ def clear_screen():
|
|||
def convert_to_bytes(size):
|
||||
"""Convert human-readable size str to bytes and return an int."""
|
||||
size = str(size)
|
||||
tmp = re.search(r'(\d+\.?\d*)\s+([KMGT]B)', size.upper())
|
||||
tmp = re.search(r'(\d+\.?\d*)\s+([PTGMKB])B?', size.upper())
|
||||
if tmp:
|
||||
size = float(tmp.group(1))
|
||||
units = tmp.group(2)
|
||||
if units == 'TB':
|
||||
size *= 1099511627776
|
||||
elif units == 'GB':
|
||||
size *= 1073741824
|
||||
elif units == 'MB':
|
||||
size *= 1048576
|
||||
elif units == 'KB':
|
||||
size *= 1024
|
||||
if units == 'P':
|
||||
size *= 1024 ** 5
|
||||
if units == 'T':
|
||||
size *= 1024 ** 4
|
||||
elif units == 'G':
|
||||
size *= 1024 ** 3
|
||||
elif units == 'M':
|
||||
size *= 1024 ** 2
|
||||
elif units == 'K':
|
||||
size *= 1024 ** 1
|
||||
elif units == 'B':
|
||||
size *= 1024 ** 0
|
||||
size = int(size)
|
||||
else:
|
||||
return -1
|
||||
|
|
|
|||
Loading…
Reference in a new issue