Home > Software design >  Building JSON object from recursive directory tree traversal
Building JSON object from recursive directory tree traversal

Time:09-17

I'm traversing a directory tree, which contains directories and files. I know I could use os.walk for this, but this is just an example of what I'm doing, and the end result has to be recursive.

The function to get the data out is below:

def walkfn(dirname):
    for name in os.listdir(dirname):
        path = os.path.join(dirname, name)
        if os.path.isdir(path):
            print(name)
            walkfn(path)
        elif os.path.isfile(path):
            print(name)

Assuming we had a directory structure such as this:

testDir/
    a/
        1/
        2/
            testa2.txt
        testa.txt
    b/
        3/
            testb3.txt
        4/

The code above would return the following:

a
testa.txt
1
2
testa2.txt
c
d
b
4
3
testb3.txt

It's doing what I would expect at this point, and the values are all correct, but I'm trying to get this data into a JSON object. I've seen that I can add these into nested dictionaries, and then convert it to JSON, but I've failed miserably at getting them into nested dictionaries using this recursive method.

The JSON I'm expecting out would be something like:

{
    "test": {
        "b": {
            "4": {},
            "3": {
                "testb3.txt": null
            }
        },
        "a": {
            "testa.txt": null,
            "1": {},
            "2": {
                "testa2.txt": null
            }
        }
    }
}

CodePudding user response:

You should pass json_data in your recursion function:

import os
from pprint import pprint
from typing import Dict


def walkfn(dirname: str, json_data: Dict=None):
    if not json_data:
        json_data = dict()
    for name in os.listdir(dirname):
        path = os.path.join(dirname, name)
        if os.path.isdir(path):
            json_data[name] = dict()
            json_data[name] = walkfn(path, json_data=json_data[name])
        elif os.path.isfile(path):
            json_data.update({name: None})
    return json_data


json_data = walkfn(dirname="your_dir_name")
pprint(json_data)

  • Related