从列表中创建目录树

时间:2016-12-27 17:00:55

标签: python

我有这个功能:

def Test2(my_path):
    def create_sound_folder_from_path(current_path):
        result = {
            'folders': {},
            'sounds': []
        }

        for entry in os.listdir(current_path):
            full_path = os.path.join(current_path, entry)
            if os.path.isdir(full_path):
                result['folders'][entry] = create_sound_folder_from_path(full_path)
            elif entry.endswith('.wav'):
                result['sounds'].append(entry)

        return result

path_to_use = my_path

result = create_sound_folder_from_path(path_to_use)

return result

它返回一个包含文件夹和文件的字典:

{
  'folders': 
      {'sounds': {'folders': {}, 'sounds': ['song1.wav', 'song2.wav']},
        'machine': {'folders': {}, 'sounds': ['song5.wav']}
       }, 
  'sounds': [] # no sounds at root
}

我的输入列表:

['sounds/sound1.wav', 'sounds/sound2.wav', 'sounds/new/sound2.wav', 'sounds/old/sound2.wav', 'machine/mach.wav']

我只想要相同的字典但是从路径列表中。有可能吗?

2 个答案:

答案 0 :(得分:2)

这里是我的贡献,这段代码使用递归调用来获取子文件夹的信息,肯定可以重写以避免主循环。

import json


def get_subdirectories(path_dict, path_list):
    if len(path_list) == 1:
        path_dict['sounds'].extend([x for x in path_list])
    elif len(path_list) > 1:
        key = path_list.pop(0)
        if key not in path_dict['folders'].keys():
            path_dict['folders'][key] = {'sounds': [], 'folders': {}}
        get_subdirectories(path_dict['folders'][key], path_list)


def main():
    directories = ['sounds/sound1.wav', 'sounds/sound2.wav',
                   'sounds/new/sound2.wav', 'sounds/old/sound2.wav', 
                   'machine/mach.wav']

    output_dict = {'sounds': [], 'folders': {}}

    for d in directories:
        root = d.split('/')[0]
        if root not in output_dict['folders'].keys():
            output_dict['folders'][root] = {'sounds': [], 'folders': {}}
        get_subdirectories(output_dict['folders'][root], d.split('/')[1:])

    print(
        json.dumps(
            output_dict,
            sort_keys=True,
            indent=4,
            separators=(',', ': ')))

结果如下:

{
    "folders": {
        "machine": {
            "folders": {},
            "sounds": [
                "mach.wav"
            ]
        },
        "sounds": {
            "folders": {
                "new": {
                    "folders": {},
                    "sounds": [
                        "sound2.wav"
                    ]
                },
                "old": {
                    "folders": {},
                    "sounds": [
                        "sound2.wav"
                    ]
                }
            },
            "sounds": [
                "sound1.wav",
                "sound2.wav"
            ]
        }
    },
    "sounds": []
}

答案 1 :(得分:1)

您是否尝试过使用expanduser中的os.path.expanduser?如果放入列表,os.walk将使用此功能。例如,要遍历我的音乐和文档文件夹,我这样做了:

from os.path import expanduser
from os.path import join

directories = [expanduser('~/Music'), expanduser('~/Documents')]

counter = 0
for item in directories:
    for subdir, dirs, files in walk(directories[counter]):
        for file in files:
            print(join(subdir, file))

正如你明确提到的os.walk,我猜你知道如何根据需要解析数据。如果没有,我可以扩展如何做到这一点。