计算每个文件夹大小
# from sign_script import delete_expire_file
# delete_expire_file(1)
import os
def get_folder_size(folder_path):
total_size = 0
for path, dirs, files in os.walk(folder_path):
for f in files:
fp = os.path.join(path, f)
total_size += os.path.getsize(fp)
return total_size
# 转换为更友好的单位
def convert_size(size_in_bytes):
# 单位列表,从字节到千兆
units = ['Bytes', 'KB', 'MB', 'GB', 'TB']
# 获取单位索引
unit_index = 0
while size_in_bytes >= 1024 and unit_index < len(units) - 1:
size_in_bytes /= 1024
unit_index += 1
# 格式化结果
size_formatted = "{:.2f} {}".format(size_in_bytes, units[unit_index])
return size_formatted
path = r'C:\Users\yys53\OneDrive'
files = os.listdir(path)
for file in files:
folder_path = os.path.join(path, file)
# print(len(str(folder_path)))
size_in_bytes = get_folder_size(folder_path)
folder_size = convert_size(size_in_bytes)
# print(folder_path, '\n')
# break
if 'Bytes' not in str(folder_size) and 'KB' not in str(folder_size):
print(folder_path, ":文件夹大小: " + folder_size, '\n\n')
本文作者: 永生
本文链接: https://yys.zone/detail/?id=283
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
评论列表 (0 条评论)