Home > Software engineering >  How to generate tree structure dictionary for path list in python
How to generate tree structure dictionary for path list in python

Time:11-08

I'm having a list variable that has all filepath's. ex:

f_list=[
  "folder1 /folder1.1/images/image1.png",
  "folder1 /folder1.1/images/image2.png",
  "folder1 /folder1.1/images/image5.png",
  "folder1 /folder1.3/Invoice_1.pdf",
  "folder1 /folder1.3/Invoice_2.pdf",
  "folder1 /folder1.3/Invoice_3.pdf",
  "folder1 /folder1.3/Invoice_4.pdf",
  "folder1 /folder1.3/Invoice_5.pdf"
   ]   

I'm trying to generate dictionary from above list like:

{
  "object_name":"",
  "object_type":"folder",
  "object_path":"",
  "object_count":8,
  "object_list":[
     {
        "object_path":"folder1 /folder1.1/images",
        "object_type":"folder",
        "object_count":3,
        "object_list":[
           {
              "object_path":"folder1 /folder1.1/images/image1.png",
              "object_name":"image1.png",
              "object_type":"file"
           },
           {
              "object_path":"folder1 /folder1.1/images/image2.png",
              "object_name":"image2.png",
              "object_type":"file"
           },
           {
              "object_path":"folder1 /folder1.1/images/image5.png",
              "object_name":"image5.png",
              "object_type":"file"
           }
        ]
     },
     {
        "object_path":"folder1 /folder1.3",
        "object_type":"folder",
        "object_count":5,
        "object_list":[
           {
              "object_path":"folder1 /folder1.3/Invoice_1.pdf",
              "object_name":"Invoice_1.pdf",
              "object_type":"file"
           },
           {
              "object_path":"folder1 /folder1.3/Invoice_2.pdf",
              "object_name":"Invoice_2.pdf",
              "object_type":"file"
           },
           {
              "object_path":"folder1 /folder1.3/Invoice_3.pdf",
              "object_name":"Invoice_3.pdf",
              "object_type":"file"
           },
           {
              "object_path":"folder1 /folder1.3/Invoice_4.pdf",
              "object_name":"Invoice_4.pdf",
              "object_type":"file"
           },
           {
              "object_path":"folder1 /folder1.3/Invoice_5.pdf",
              "object_name":"Invoice_5.pdf",
              "object_type":"file"
           }
        ]
     }
  ]
  
}

Is there any library is available for this case? I'm still trying to get the output in this format. I will post the code snippet once I achieve it.

also Can anyone suggest the way to achieve this

Thanks in advance!.

CodePudding user response:

You could use a helper dictionary keyed by path to remember the nodes you have created, so that you can put a child in the parent's list of children.

Here is some code:

def maketree(lst):
    mapper = {}
    for path in lst:
        parent = None
        for key in (path[:i] for i, c in enumerate(path) if i == 0 or c == "/"):
            if key not in mapper:
                node = {
                    "object_type": "folder",
                    "object_path": key,
                    "object_count": 0,
                    "object_list": []
                }
                mapper[key] = node
                if key:
                    mapper[parent]["object_list"].append(node)
            mapper[key]["object_count"]  = 1
            parent = key
        node = {
            "object_type": "file",
            "object_path": path,
            "object_name": path[(len(key) or -1)   1:]
        }
        mapper[path] = node
        mapper[parent]["object_list"].append(node)
    return mapper[""]

You can use it as follows:

# Example run
f_list = [
    "folder1 /folder1.1/images/image1.png",
    "folder1 /folder1.1/images/image2.png",
    "folder1 /folder1.1/images/image5.png",
    "folder1 /folder1.3/Invoice_1.pdf",
    "folder1 /folder1.3/Invoice_2.pdf",
    "folder1 /folder1.3/Invoice_3.pdf",
    "folder1 /folder1.3/Invoice_4.pdf",
    "folder1 /folder1.3/Invoice_5.pdf"
]   

tree = maketree(f_list)

# Pretty print the tree
import json
print(json.dumps(tree, indent=2))

This will produce more levels than you have in your question, as also a level is created for folder1 , and for folder1 /folder1.1. I consider that is more consistent:

{
  "object_type": "folder",
  "object_path": "",
  "object_count": 8,
  "object_list": [
    {
      "object_type": "folder",
      "object_path": "folder1 ",
      "object_count": 8,
      "object_list": [
        {
          "object_type": "folder",
          "object_path": "folder1 /folder1.1",
          "object_count": 3,
          "object_list": [
            {
              "object_type": "folder",
              "object_path": "folder1 /folder1.1/images",
              "object_count": 3,
              "object_list": [
                {
                  "object_type": "file",
                  "object_path": "folder1 /folder1.1/images/image1.png",
                  "object_name": "image1.png"
                },
                {
                  "object_type": "file",
                  "object_path": "folder1 /folder1.1/images/image2.png",
                  "object_name": "image2.png"
                },
                {
                  "object_type": "file",
                  "object_path": "folder1 /folder1.1/images/image5.png",
                  "object_name": "image5.png"
                }
              ]
            }
          ]
        },
        {
          "object_type": "folder",
          "object_path": "folder1 /folder1.3",
          "object_count": 5,
          "object_list": [
            {
              "object_type": "file",
              "object_path": "folder1 /folder1.3/Invoice_1.pdf",
              "object_name": "Invoice_1.pdf"
            },
            {
              "object_type": "file",
              "object_path": "folder1 /folder1.3/Invoice_2.pdf",
              "object_name": "Invoice_2.pdf"
            },
            {
              "object_type": "file",
              "object_path": "folder1 /folder1.3/Invoice_3.pdf",
              "object_name": "Invoice_3.pdf"
            },
            {
              "object_type": "file",
              "object_path": "folder1 /folder1.3/Invoice_4.pdf",
              "object_name": "Invoice_4.pdf"
            },
            {
              "object_type": "file",
              "object_path": "folder1 /folder1.3/Invoice_5.pdf",
              "object_name": "Invoice_5.pdf"
            }
          ]
        }
      ]
    }
  ]
}

CodePudding user response:

https://gist.github.com/RameshPonnusami/a9ae7893f0ff51914f3c4a3c1abc5b6f.js

Check out this code snippet.

  • Related