1

This is more or less reverse question to List directory tree structure in python? and this answer https://stackoverflow.com/a/51991554/7422721

Given that we have text art style directory tree (ASCII, but not limited to), generated as an output from tree program or equivalent Python script:

a/
├── b/
│   └── c/
│       ├── file_1
│       └── file_2
└── d/

Each file name ends with one of the '/', '=', '*', '@', '|' or '>' indicating file type, as with the ls -F or tree -F convention. So file name ending with / would be directory, = for socket etc. If there is no ending, we assume that it is regular file.

How can I read this back into native data structure, for example:

[
  "a": [
    "b": [
      "c": [
        "file_1", "file_2"
      ]
    ],
    "d": []
  ]
]  

Or to other object that could represent in memory filesystem?

Goal here would be to create readable unit tests for script that manipulates files on disk.

Majus Misiak
  • 643
  • 7
  • 14
  • So you want to convert ASCII tree to dict? – Alderven Feb 19 '19 at 14:29
  • @Alderven This is a bit specific - I want to convert text tree to Python object. It does not necessarily have to be nested list/dict, but I think it would be the simplest one to impement. – Majus Misiak Feb 19 '19 at 14:34

1 Answers1

0

I was able to convert sample tree from your question to dict format:

{'a': {'b': {'c': {'file_1': {}, 'file_2': {}}}, 'd': {}}}

Script:

import re

def find_children(tree):
    result = {}
    for i, line in enumerate(tree.split('\n')):
        child = re.findall('^\w+', line)
        if child:
            parent = child[0]
            child_tree = '\n'.join([x[len(parent)+3:] for x in tree.split('\n')[i+1:]])
            result[parent] = find_children(child_tree)
    return result

with open('tree.txt', 'r', encoding='utf-8') as f:
    tree_dict = find_children(f.read())
Alderven
  • 7,569
  • 5
  • 26
  • 38
  • I like the idea of ignoring special characters like `├ └ │ ─`. But it will fail on anything little more complicated than the provided sample with a,b,c.... Try for example using it on the output of `tree /etc` :) – Majus Misiak Feb 22 '19 at 18:52