# -*- coding: utf-8 -*-
import os
from collections import defaultdict
def analyze_file_formats(directory):
# 用于存储文件格式及其路径的字典
file_formats = defaultdict(list)
# 遍历目录及其子目录
for root, _, files in os.walk(directory):
for file in files:
# 获取文件的格式(扩展名)
file_extension = os.path.splitext(file)[1]
# 将文件路径添加到对应格式的列表中
file_formats[file_extension].append(os.path.join(root, file))
# 打印每种格式的文件数量及其路径
for file_format, paths in file_formats.items():
print("文件格式: {}, 数量: {}".format(file_format or '无扩展名', len(paths)))
for path in paths:
print(" 路径: {}".format(path))
if __name__ == "__main__":
# 设定要分析的目录,这里设为 /home
directory_to_analyze = "/home"
analyze_file_formats(directory_to_analyze)
统计/home下面的所有文件列表
最新推荐文章于 2025-12-09 09:40:28 发布
3531

被折叠的 条评论
为什么被折叠?



