我试图从git存储库获取目录和文件的字典,如下所示:
{"dir1": ["file1", "file2", "dir2", "dir3"], "dir2": {"file3", "dir4"}}
我知道如何获取文件列表,问题是如何将其转换为字典,就像这样。
我的文件列表如下所示:
["dir/dir1/file1", "dir/dir1/file2", "dir/dir1/dir2/somefile"]
答案 0 :(得分:0)
现在感觉很蠢...... 我用了这段代码:
directories = {'': set()}
for f in files:
splitted = f.split("/")
directories[''].add(splitted[0])
for i in range(len(splitted)):
if i == len(splitted)-1:
break
d = ''
for j in splitted[0:i+1]:
d += j + os.path.sep
d = d[0:-1]
if not d in directories:
directories[d] = set()
directories[d].add(splitted[i+1])