Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I rebuild a directory structure into a dictionary?

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!

like image 941
SlavaCat Avatar asked Jan 21 '26 07:01

SlavaCat


1 Answers

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.

like image 112
BlackMath Avatar answered Jan 22 '26 21:01

BlackMath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!