16 lines
448 B
Python
16 lines
448 B
Python
import os
|
|
|
|
|
|
def format_size(size_bytes):
|
|
"""格式化文件大小为人类可读的形式"""
|
|
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
|
|
if size_bytes < 1024:
|
|
return f"{size_bytes:.2f} {unit}"
|
|
size_bytes /= 1024
|
|
return f"{size_bytes:.2f} PB"
|
|
|
|
|
|
def ensure_directory_exists(directory):
|
|
"""确保目录存在,如果不存在则创建"""
|
|
if not os.path.exists(directory):
|
|
os.makedirs(directory)
|