You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python将Unix ls输出转换为JSON文件夹树

将ls输出的文件路径汇总为JSON树形结构

要解决单个路径生成独立树、无法合并的问题,核心是维护一个全局根节点,逐个将每个路径的层级节点插入到根节点中。以下是实现代码:

import json

input_str_list = [
    '2023-06-27 13:10:31        772 dexter/lab_report/inventory/rider/jointball/rotatable/archive/LOGS/hammer_file_download_202105011701.log',
    '2023-06-27 13:10:31        772 dexter/lab_report/inventory/rider/jointball/rotatable/LOGS/hammer_file_download_202112021849.log',
    '2023-06-27 13:10:31        146 dexter/lab_report/inventory/rider/jointball/rotatable/SUMMARY/SUMMARY_rotatable_report_2022-04-01.csv',
    '2023-06-27 13:10:32        145 dexter/lab_report/consumable/fastner/jointball/stopscrew/SUMMARY/SUMMARY_stopscrew/SUMMARY_stopscrew_report_2022-04-11.csv'
]

# 初始化树形结构的根节点
root = {}

for inp_str in input_str_list:
    # 拆分字符串并提取文件路径(split()自动处理多空格)
    split_parts = inp_str.split()
    file_path = split_parts[-1]
    # 将路径拆分为目录和文件名分段
    path_segments = file_path.split('/')
    
    current_node = root
    # 遍历所有目录分段(排除最后一个文件名)
    for segment in path_segments[:-1]:
        # 若当前目录不存在则创建空字典
        if segment not in current_node:
            current_node[segment] = {}
        # 移动到下一层目录节点
        current_node = current_node[segment]
    # 添加文件名作为叶子节点
    filename = path_segments[-1]
    current_node[filename] = filename

# 生成格式化的JSON输出
merged_tree_json = json.dumps(root, indent=4)
print(merged_tree_json)

代码说明

  1. 全局根节点:用root字典存储整个树形结构,所有路径都插入到这个根节点下。
  2. 路径提取:使用split()处理输入字符串,自动忽略多个空格,直接取最后一个元素作为完整文件路径。
  3. 层级遍历插入:
    • 遍历路径的目录分段,逐层检查并创建不存在的目录节点(空字典)。
    • 最后将文件名作为叶子节点添加到对应目录下,值设为文件名(匹配原代码的处理逻辑)。
  4. JSON格式化:用json.dumps的indent参数生成可读性强的树形JSON字符串。

输出示例

运行后将得到合并后的统一树形JSON:

{
    "dexter": {
        "lab_report": {
            "inventory": {
                "rider": {
                    "jointball": {
                        "rotatable": {
                            "archive": {
                                "LOGS": {
                                    "hammer_file_download_202105011701.log": "hammer_file_download_202105011701.log",
                                    "hammer_file_download_202112021849.log": "hammer_file_download_202112021849.log"
                                }
                            },
                            "SUMMARY": {
                                "SUMMARY_rotatable_report_2022-04-01.csv": "SUMMARY_rotatable_report_2022-04-01.csv"
                            }
                        }
                    }
                }
            },
            "consumable": {
                "fastner": {
                    "jointball": {
                        "stopscrew": {
                            "SUMMARY": {
                                "SUMMARY_stopscrew": {
                                    "SUMMARY_stopscrew_report_2022-04-11.csv": "SUMMARY_stopscrew_report_2022-04-11.csv"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者damd biker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 07:38:18