Home > Back-end >  How would I rebuild a directory structure into a dictionary?
How would I rebuild a directory structure into a dictionary?

Time:10-17

I'd like to take a single folder path (root) and then put all the file paths into a dictionary that resembles the original directory structure.

Ex: I have a folder that looks like this:

root
-sub1
--someFile.txt
--someFile2.txt
-sub2
--subsub1
---veryNested.txt
--someFile3.txt
-someFile4.txt

I would like the dictionary to look like this:

{'root': {
    '.dirs': {
        'sub1':{
            '.dirs':{},
            '.files':['someFile.txt', 'someFile2.txt']
        },
        'sub2':{
            '.dirs':{
                'subsub1':{
                    '.dirs':{},
                    '.files':['veryNested.txt']
                }
            },
            '.files':['someFile3.txt']
        }
    },
    '.files':['someFile4.txt']
}

I've been looking around and I can't really find a good general answer to this problem. Could someone point me towards some good resources, or give a brief and general explanation on what the code would look like? I'd like to figure this out without someone holding my hand 100% of the way, or just giving me the solution. Please let me know if more clarification is needed!

CodePudding user response:

There are many ways to get a representation of the directory structure.

The following fuction uses a recursive approach to list your directory structure into a json object:

import os
import json

def path_to_dict(path):
    d = {'name': os.path.basename(path)}
    if os.path.isdir(path):
        d['type'] = "folder"
        d['content'] = [path_to_dict(os.path.join(path, x)) for x in os.listdir(path)]
    else:
        d['type'] = "file"
    return d
# string rapresentation 
dict_tree = json.dumps(path_to_dict('C:/Users/foo/Desktop/test'))
# convert in json
json = json.loads(dict_tree )

Output:

{'name': 'test',
 'type': 'folder',
 'content': [{'name': 'subfolder_1',
   'type': 'folder',
   'content': [{'name': 'test_file_1.txt', 'type': 'file'},
    {'name': 'test_file_2.txt', 'type': 'file'}]},
  {'name': 'subfolder_2',
   'type': 'folder',
   'content': [{'name': 'test_file_3.txt', 'type': 'file'}]}]}

EXTRA: If you're working on a Linux machine, you can get the same with tree tool. In order to list the files and subfolders of a specific directory, you can specify the directory name or path through the following command syntax:

tree -J folder_name

-J argoument is used for a json rapresentation.

CodePudding user response:

The following code converts a directory path into a readable dictionary:

Thanks to @BlackMath for pointing me in the right direction!

import os
from os import path

def dirToDict(dirPath):
    d = {}
    for i in [os.path.join(dirPath, i) for i in os.listdir(dirPath) if os.path.isdir(os.path.join(dirPath, i))]:
        d[os.path.basename(i)] = dirToDict(i) # You can remove the 'basename' to get the full directory path
    d['.files'] = [i for i in os.listdir(dirPath) if os.path.isfile(os.path.join(dirPath, i))] # You can add a os.path.join(dirPath, i) here to get full file name
    return d
  • Related