如何为多个文件创建 table 并递归输入结果

How to create a table and enter result recursively for multiple files

我在 folder1 中的 folder2 中的 folder3 中有很多 .txt 文件。所有文件夹都有很多子文件夹,但最主要的是有 3 个文件夹层次结构,然后在最后一个子文件夹中有 txt 个文件。现在我在 txt 文件上确实有 python 到 运行 的代码,并且得到的结果是每个文件的一个或两个输出。我想创建一个 table/dataframe/csv 文件,其中 table 中的一行包含 'folder1'、'folder2'、'folder3' 列。 .txt 文件的名称和 python 代码的结果都在同一行中。

类似这样的东西,还没有测试过,但你会知道该怎么做。

init_dir='D:\Users\AChauh11\PycharmProjects\untitled1'
dict = {}
def walk(init_dir):
    list = []
    for file in os.listdir(init_dir+'\'+init_dir):
        if os.path.isdir(file):
            walk(init_dir+'\'+file)
        else:
            list.append(file)
            dict[init_dir]=list
    return dict

这将 return 一个字典,您可以使用它在 pandas 或类似的东西中创建 table。