I have the following items in a list with the format of directories structure.
[
'fold/2021/',
'fold/2021/11/',
'fold/2021/11/01/',
'fold/2021/11/01/123.gz',
'fold/2021/11/01/345.gz',
'fold/2021/12/',
'fold/2021/12/02/',
'fold/2022/'
]
I need this in the following nested dictionary structure:
{
"fold": {
"2021": {
"11": {
"01": {
"123.gz": None,
"345.gz": None
}
},
"12": {
"02": {}
}
},
"2022": {}
}
}
I tried a lot with recursion and some other methods, but I am not getting this structure.
Here is what I tried:
def get_directory_structure(path):
global main_dict
local_dict = {}
a = path.rstrip('/').split('/')
local_dict.setdefault(a[0], {})
if len(a) > 1:
return_dict = get_directory_structure(path[path.find('/') 1:])
local_dict[a[0]] = return_dict
if a[0] == 'fold':
main_dict.update(**local_dict)
return local_dict
main_dict = {}
for path in paths:
get_directory_structure(main_dict, path)
print(main_dict)
Please help me with this. Thank you
Note:- I don't have the folder on my PC. I just have the items in the list
CodePudding user response:
You can try like this, not using recursion but using *
-unpacking to separate the items into the file (or ''
) and the path leading up to that, and using setdefault
do "expand" deeper levels of the dict, if they do not exist yet, and finally add the file, if any.
res = {}
for item in lst:
d = res
*path, last = item.split("/")
for p in path:
d = d.setdefault(p, {})
if last != "":
d[last] = None
Afterwards, res
should be your desired result:
{'fold': {'2021': {'11': {'01': {'123.gz': None, '345.gz': None}}, '12': {'02': {}}}, '2022': {}}}