是否有 python 模块从 paths/files 的列表或索引创建文件树(非本地系统)

Is there a python module to create filetree from a list or index of paths/files (not local system)

正在处理一个学校项目...我有一个 python 列表对象(从文本文件中获取),其中包含整个目录列表(大约 40 万个项目)。是否有模块可以自动将此列表或文本文件组织成文件树结构?

例如,根开始列表“/”。接着是其中的第一个文件夹,一直到路径 "/folder1/sub-folder_last/lastfile.txt

中的最后一个文件

这一直到最后一个项目 "/last_folder_in_root" 到那个 "/last_folder_in_root/last_sub_folder/last_file.txt"

中的最后一个子文件夹

我一直在寻找一个好的起点,但搜索中的歧义让我除了 os、os 步行项目外一无所获。希望已经有一些东西可以 运行 通过这个并用标签或类似的东西分隔子项目。

最终输出类似于:

/
    /first_folder
        /first_sub_folder
            /file.txt
    /second_folder
    /last_folder
        /last_sub_fodler
             /last_file.txt

我搜索了多个库,但找不到支持此功能的库。这不涉及os.walk,它不是针对本地文件系统的。它来自 txt 文件或列表。

基本上是试图找到与 os.walk 输出类似的东西,但从列表或文件中获取信息,而不是从本地系统中获取信息。有什么想法或方向吗?

你可以用一些逻辑来解决这个问题

with open('filename.txt') as in_file:
    for line in in_file.readlines():
        as_list = line.split('/')
        # special case for the root
        if len(as_list) == 2 and as_list[0] == '' and as_list[-1] == '\n':
            indent = 0
        else:
            indent = (len(as_list) - 1) * 4 + len(as_list[-1]) + 1
        output = '/{}'.format(as_list[-1].strip('\n'))
        print(output.rjust(indent))