代码分享
import os
from pprint import pprint
def get_folder_size(folder_path):
total_size = 0
for dirpath, dirnames, filenames in os.walk(folder_path):
for filename in filenames:
file_path = os.path.join(dirpath, filename)
total_size += os.path.getsize(file_path)
return total_size // 1000
def get_subfolder_sizes(folder_path):
subfolder_sizes = {}
for name in os.listdir(folder_path):
if os.path.isdir(os.path.join(folder_path, name)):
subfolder_path = os.path.join(folder_path, name)
folder_size = get_folder_size(subfolder_path)
subfolder_sizes[subfolder_path] = folder_size
return dict(sorted(subfolder_sizes.items(), key=lambda x: x[1], reverse=True))
if __name__ == '__main__':
file_path = r'C:\Program Files (x86)'
result = get_subfolder_sizes(file_path)
pprint(result, sort_dicts=False)
结果示例:
![](https://img-blog.csdnimg.cn/direct/b5f19169cb324b98a94a776f2f91f7b2.png)