mirror of
https://github.com/ngfchl/ptools
synced 2023-07-10 13:41:22 +08:00
优化文件大小获取失败时,直接返回0
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user