如何在 python 中创建用于存储目录结构的嵌套字典?
How to create a nested dictionary for storing directory structure in python?
假设以下目录结构:-
|--apc
| |--data
| | |--datainstaller.zip
| | |--rs_details
| | | |--readme.txt
| |--rem
| | |--15.0.x
| | | |--15.0.1
| | | | |--dataconversion
| | | | | |--details.txt
| | | |--15.0.2
| | | | |--dataconversion
| | | | | |--details.txt
| | |--alloc_details.txt
| |--res
| | |--resapp.zip
|--REIM
| |--dataconversion
| | |--dataconversioninstaller.zip
| | |--rs_details.txt
| |--reim_bkp.zip
| |--hotfix
| | |--mergedRC
| | | |--alloc
| | | | |--p231.zip
|--testfile1.txt
|--testfile2.txt
作为绝对路径给出(从根目录开始,以深度优先的方式)在python 列表为(即,路径在列表中作为字符串给出):-
['apc/','apc/data/', 'apc/data/datainstaller.zip', 'apc/data/rs_details/', 'apc/data/rs_details/readme.txt', 'apc/rem/', 'apc/rem/15.0.x/', 'apc/rem/15.0.x/15.0.1/', 'apc/rem/15.0.x/15.0.1/dataconversion/', 'apc/rem/15.0.x/15.0.1/dataconversion/details.txt', 'apc/rem/15.0.x/15.0.2/', 'apc/rem/15.0.x/15.0.2/dataconversion/', 'apc/rem/15.0.x/15.0.2/dataconversion/details.txt', 'apc/rem/alloc_details.txt', 'apc/res/', 'apc/res/resapp.zip', 'REIM/', 'REIM/dataconversion/', 'REIM/dataconversion/dataconversioninstaller.zip', 'REIM/dataconversion/rs_details.txt', 'REIM/reim_bkp.zip', 'REIM/hotfix/', 'REIM/hotfix/mergedRC/', 'REIM/hotfix/mergedRC/alloc', 'REIM/hotfix/mergedRC/alloc/p231.zip', 'testfile1.txt', 'testfile2.txt']
我想创建一个嵌套字典来存储这个目录结构,它应该是这样的:-
{
'apc/': {
'apc/data/':{
'apc/data/datainstaller.zip' : {}
'apc/data/rs_details/' : {
'apc/data/rs_details/readme.txt' : {}
}
'apc/rem/':{
'apc/rem/15.0.x' : {
'apc/rem/15.0.x/15.0.1/': {
'apc/rem/15.0.x/15.0.1/dataconversion' : { 'apc/rem/15.0.x/15.0.1/dataconversion/details.txt' : {} }
}
'apc/rem/15.0.x/15.0.2/': {
'apc/rem/15.0.x/15.0.2/dataconversion' : { 'apc/rem/15.0.x/15.0.2/dataconversion/details.txt' : {} }
}
}
}
}
}
我尝试通过在路径中使用参数作为 /
的数量并在较低级别的目录路径中查找前缀来迭代和递归地执行此操作,但它导致嵌套字典不一致,我无法如上所示获得所需的。
谁能帮我解决这个问题?
[编辑:目录的路径字符串总是以 /
结尾,而文件的路径字符串不]
这样就可以了:
import os, json
directory_dict = {}
path_list = ['apc/','apc/data/', 'apc/data/datainstaller.zip', 'apc/data/rs_details/', 'apc/data/rs_details/readme.txt', 'apc/rem/', 'apc/rem/15.0.x/', 'apc/rem/15.0.x/15.0.1/', 'apc/rem/15.0.x/15.0.1/dataconversion/', 'apc/rem/15.0.x/15.0.1/dataconversion/details.txt', 'apc/rem/15.0.x/15.0.2/', 'apc/rem/15.0.x/15.0.2/dataconversion/', 'apc/rem/15.0.x/15.0.2/dataconversion/details.txt', 'apc/rem/alloc_details.txt', 'apc/res/', 'apc/res/resapp.zip', 'REIM/', 'REIM/dataconversion/', 'REIM/dataconversion/dataconversioninstaller.zip', 'REIM/dataconversion/rs_details.txt', 'REIM/reim_bkp.zip', 'REIM/hotfix/', 'REIM/hotfix/mergedRC/', 'REIM/hotfix/mergedRC/alloc', 'REIM/hotfix/mergedRC/alloc/p231.zip', 'testfile1.txt', 'testfile2.txt']
for item in path_list:
path, filename = os.path.split(item)
split_path = path.split('/')
# directory_level is a pointer into the directory_dict
directory_level = directory_dict
# current_directory is a string to hold the path name as we build it up again from iterating the split_path list
current_directory = ''
for path_item in split_path:
# build up the current_directory
current_directory += path_item + '/'
if current_directory not in directory_level:
# insert new dictionary at this level
directory_level[current_directory] = {}
# set the directory_level pointer to new level
directory_level = directory_level[current_directory]
if filename:
# add the whole item into the dictionary at this level
directory_level[item] = {}
print(json.dumps(directory_dict, indent=4))
并且输出:
{
"apc/": {
"apc/data/": {
"apc/data/datainstaller.zip": {},
"apc/data/rs_details/": {
"apc/data/rs_details/readme.txt": {}
}
},
"apc/rem/": {
"apc/rem/15.0.x/": {
"apc/rem/15.0.x/15.0.1/": {
"apc/rem/15.0.x/15.0.1/dataconversion/": {
"apc/rem/15.0.x/15.0.1/dataconversion/details.txt": {}
}
},
"apc/rem/15.0.x/15.0.2/": {
"apc/rem/15.0.x/15.0.2/dataconversion/": {
"apc/rem/15.0.x/15.0.2/dataconversion/details.txt": {}
}
}
},
"apc/rem/alloc_details.txt": {}
},
"apc/res/": {
"apc/res/resapp.zip": {}
}
},
"REIM/": {
"REIM/dataconversion/": {
"REIM/dataconversion/dataconversioninstaller.zip": {},
"REIM/dataconversion/rs_details.txt": {}
},
"REIM/reim_bkp.zip": {},
"REIM/hotfix/": {
"REIM/hotfix/mergedRC/": {
"REIM/hotfix/mergedRC/alloc": {},
"REIM/hotfix/mergedRC/alloc/": {
"REIM/hotfix/mergedRC/alloc/p231.zip": {}
}
}
}
},
"/": {
"testfile1.txt": {},
"testfile2.txt": {}
}
}
假设以下目录结构:-
|--apc
| |--data
| | |--datainstaller.zip
| | |--rs_details
| | | |--readme.txt
| |--rem
| | |--15.0.x
| | | |--15.0.1
| | | | |--dataconversion
| | | | | |--details.txt
| | | |--15.0.2
| | | | |--dataconversion
| | | | | |--details.txt
| | |--alloc_details.txt
| |--res
| | |--resapp.zip
|--REIM
| |--dataconversion
| | |--dataconversioninstaller.zip
| | |--rs_details.txt
| |--reim_bkp.zip
| |--hotfix
| | |--mergedRC
| | | |--alloc
| | | | |--p231.zip
|--testfile1.txt
|--testfile2.txt
作为绝对路径给出(从根目录开始,以深度优先的方式)在python 列表为(即,路径在列表中作为字符串给出):-
['apc/','apc/data/', 'apc/data/datainstaller.zip', 'apc/data/rs_details/', 'apc/data/rs_details/readme.txt', 'apc/rem/', 'apc/rem/15.0.x/', 'apc/rem/15.0.x/15.0.1/', 'apc/rem/15.0.x/15.0.1/dataconversion/', 'apc/rem/15.0.x/15.0.1/dataconversion/details.txt', 'apc/rem/15.0.x/15.0.2/', 'apc/rem/15.0.x/15.0.2/dataconversion/', 'apc/rem/15.0.x/15.0.2/dataconversion/details.txt', 'apc/rem/alloc_details.txt', 'apc/res/', 'apc/res/resapp.zip', 'REIM/', 'REIM/dataconversion/', 'REIM/dataconversion/dataconversioninstaller.zip', 'REIM/dataconversion/rs_details.txt', 'REIM/reim_bkp.zip', 'REIM/hotfix/', 'REIM/hotfix/mergedRC/', 'REIM/hotfix/mergedRC/alloc', 'REIM/hotfix/mergedRC/alloc/p231.zip', 'testfile1.txt', 'testfile2.txt']
我想创建一个嵌套字典来存储这个目录结构,它应该是这样的:-
{
'apc/': {
'apc/data/':{
'apc/data/datainstaller.zip' : {}
'apc/data/rs_details/' : {
'apc/data/rs_details/readme.txt' : {}
}
'apc/rem/':{
'apc/rem/15.0.x' : {
'apc/rem/15.0.x/15.0.1/': {
'apc/rem/15.0.x/15.0.1/dataconversion' : { 'apc/rem/15.0.x/15.0.1/dataconversion/details.txt' : {} }
}
'apc/rem/15.0.x/15.0.2/': {
'apc/rem/15.0.x/15.0.2/dataconversion' : { 'apc/rem/15.0.x/15.0.2/dataconversion/details.txt' : {} }
}
}
}
}
}
我尝试通过在路径中使用参数作为 /
的数量并在较低级别的目录路径中查找前缀来迭代和递归地执行此操作,但它导致嵌套字典不一致,我无法如上所示获得所需的。
谁能帮我解决这个问题?
[编辑:目录的路径字符串总是以 /
结尾,而文件的路径字符串不]
这样就可以了:
import os, json
directory_dict = {}
path_list = ['apc/','apc/data/', 'apc/data/datainstaller.zip', 'apc/data/rs_details/', 'apc/data/rs_details/readme.txt', 'apc/rem/', 'apc/rem/15.0.x/', 'apc/rem/15.0.x/15.0.1/', 'apc/rem/15.0.x/15.0.1/dataconversion/', 'apc/rem/15.0.x/15.0.1/dataconversion/details.txt', 'apc/rem/15.0.x/15.0.2/', 'apc/rem/15.0.x/15.0.2/dataconversion/', 'apc/rem/15.0.x/15.0.2/dataconversion/details.txt', 'apc/rem/alloc_details.txt', 'apc/res/', 'apc/res/resapp.zip', 'REIM/', 'REIM/dataconversion/', 'REIM/dataconversion/dataconversioninstaller.zip', 'REIM/dataconversion/rs_details.txt', 'REIM/reim_bkp.zip', 'REIM/hotfix/', 'REIM/hotfix/mergedRC/', 'REIM/hotfix/mergedRC/alloc', 'REIM/hotfix/mergedRC/alloc/p231.zip', 'testfile1.txt', 'testfile2.txt']
for item in path_list:
path, filename = os.path.split(item)
split_path = path.split('/')
# directory_level is a pointer into the directory_dict
directory_level = directory_dict
# current_directory is a string to hold the path name as we build it up again from iterating the split_path list
current_directory = ''
for path_item in split_path:
# build up the current_directory
current_directory += path_item + '/'
if current_directory not in directory_level:
# insert new dictionary at this level
directory_level[current_directory] = {}
# set the directory_level pointer to new level
directory_level = directory_level[current_directory]
if filename:
# add the whole item into the dictionary at this level
directory_level[item] = {}
print(json.dumps(directory_dict, indent=4))
并且输出:
{
"apc/": {
"apc/data/": {
"apc/data/datainstaller.zip": {},
"apc/data/rs_details/": {
"apc/data/rs_details/readme.txt": {}
}
},
"apc/rem/": {
"apc/rem/15.0.x/": {
"apc/rem/15.0.x/15.0.1/": {
"apc/rem/15.0.x/15.0.1/dataconversion/": {
"apc/rem/15.0.x/15.0.1/dataconversion/details.txt": {}
}
},
"apc/rem/15.0.x/15.0.2/": {
"apc/rem/15.0.x/15.0.2/dataconversion/": {
"apc/rem/15.0.x/15.0.2/dataconversion/details.txt": {}
}
}
},
"apc/rem/alloc_details.txt": {}
},
"apc/res/": {
"apc/res/resapp.zip": {}
}
},
"REIM/": {
"REIM/dataconversion/": {
"REIM/dataconversion/dataconversioninstaller.zip": {},
"REIM/dataconversion/rs_details.txt": {}
},
"REIM/reim_bkp.zip": {},
"REIM/hotfix/": {
"REIM/hotfix/mergedRC/": {
"REIM/hotfix/mergedRC/alloc": {},
"REIM/hotfix/mergedRC/alloc/": {
"REIM/hotfix/mergedRC/alloc/p231.zip": {}
}
}
}
},
"/": {
"testfile1.txt": {},
"testfile2.txt": {}
}
}