将路径列表转换为python中的字典

ACB*_*Bob 3 python recursion dictionary filepath

我正在用Python编写一个程序,我需要与“假设”路径(也就是实际文件系统上不存在也不会存在的路径)进行交互,并且我需要能够listdir像平常一样使用它们(path['directory']将返回每个目录内的项目,例如os.listdir())。

我想出的解决方案是将字符串路径列表转换为字典的字典。我想出了这个递归函数(它在一个类中):

    def DoMagic(self,paths):
        structure = {}
        if not type(paths) == list:
            raise ValueError('Expected list Value, not '+str(type(paths)))
        for i in paths:
            print(i)
            if i[0] == '/': #Sanity check
                print('trailing?',i) #Inform user that there *might* be an issue with the input.
                i[0] = ''
            i = i.split('/') #Split it, so that we can test against different parts.
            if len(i[1:]) > 1: #Hang-a-bout, there's more content!
                structure = {**structure, **self.DoMagic(['/'.join(i[1:])])}
            else:
                structure[i[1]] = i[1]
Run Code Online (Sandbox Code Playgroud)

但是当我用['foo/e.txt','foo/bar/a.txt','foo/bar/b.cfg','foo/bar/c/d.txt']输入来运行它时,我得到:

{'e.txt': 'e.txt', 'a.txt': 'a.txt', 'b.cfg': 'b.cfg', 'd.txt': 'd.txt'}
Run Code Online (Sandbox Code Playgroud)

我希望能够path['foo']['bar']获取foo/bar/目录中的所有内容。

编辑:

更理想的输出是:

{'e.txt': 'e.txt', 'a.txt': 'a.txt', 'b.cfg': 'b.cfg', 'd.txt': 'd.txt'}
Run Code Online (Sandbox Code Playgroud)

Err*_*rse 7

编辑 10-14-22我的第一个答案符合OP的要求,但并不是真正理想的方法,也不是最干净的输出。由于这个问题似乎更常用,请参阅下面的更简洁的方法,该方法对 Unix/Windows 路径更具弹性,并且输出字典更有意义。

from pathlib import Path
import json

def get_path_dict(paths: list[str | Path]) -> dict:
    """Builds a tree like structure out of a list of paths"""
    def _recurse(dic: dict, chain: tuple[str, ...] | list[str]):
        if len(chain) == 0:
            return
        if len(chain) == 1:
            dic[chain[0]] = None
            return
        key, *new_chain = chain
        if key not in dic:
            dic[key] = {}
        _recurse(dic[key], new_chain)
        return

    new_path_dict = {}
    for path in paths:
        _recurse(new_path_dict, Path(path).parts)
    return new_path_dict

l1 = ['foo/e.txt', 'foo/bar/a.txt', 'foo/bar/b.cfg', Path('foo/bar/c/d.txt'), 'test.txt']
result = get_path_dict(l1)
print(json.dumps(result, indent=2))
Run Code Online (Sandbox Code Playgroud)

输出:

{
  "foo": {
    "e.txt": null,
    "bar": {
      "a.txt": null,
      "b.cfg": null,
      "c": {
        "d.txt": null
      }
    }
  },
  "test.txt": null
}
Run Code Online (Sandbox Code Playgroud)

较旧的方法

这个怎么样。它会得到您想要的输出,但是树结构可能更清晰。

from collections import defaultdict
import json

def nested_dict():
   """
   Creates a default dictionary where each value is an other default dictionary.
   """
   return defaultdict(nested_dict)

def default_to_regular(d):
    """
    Converts defaultdicts of defaultdicts to dict of dicts.
    """
    if isinstance(d, defaultdict):
        d = {k: default_to_regular(v) for k, v in d.items()}
    return d

def get_path_dict(paths):
    new_path_dict = nested_dict()
    for path in paths:
        parts = path.split('/')
        if parts:
            marcher = new_path_dict
            for key in parts[:-1]:
               marcher = marcher[key]
            marcher[parts[-1]] = parts[-1]
    return default_to_regular(new_path_dict)
            
l1 = ['foo/e.txt','foo/bar/a.txt','foo/bar/b.cfg','foo/bar/c/d.txt', 'test.txt']
result = get_path_dict(l1)
print(json.dumps(result, indent=2))
Run Code Online (Sandbox Code Playgroud)

输出:

{
  "foo": {
    "e.txt": "e.txt",
    "bar": {
      "a.txt": "a.txt",
      "b.cfg": "b.cfg",
      "c": {
        "d.txt": "d.txt"
      }
    }
  },
  "test.txt": "test.txt"
}
Run Code Online (Sandbox Code Playgroud)