优化文件大小获取失败时,直接返回0

This commit is contained in:
ngfchl
2022-11-24 17:18:26 +08:00
parent 21621c8363
commit 03c267e5c9

View File

@@ -57,6 +57,8 @@ class FileSizeConvert:
@staticmethod
def parse_2_byte(file_size: str):
if not file_size:
return 0
"""将文件大小字符串解析为字节"""
regex = re.compile(r'(\d+(?:\.\d+)?)\s*([kmgtp]?b)', re.IGNORECASE)
@@ -67,11 +69,13 @@ class FileSizeConvert:
@staticmethod
def parse_2_file_size(byte: int):
if not byte:
return '0B'
units = ["B", "KB", "MB", "GB", "TB", "PB", 'EB']
size = 1024.0
for i in range(len(units)):
if (byte / size) < 1:
return "%.3f%s" % (byte, units[i])
return "%.3f %s" % (byte, units[i])
byte = byte / size