For clarity I do not show reading of file, but hard-code it, so say you use variable content to store what was read from file:
content = ''' .0. Folder Name a
.1. Folder Name b
.2. Folder Name c
.2. Folder Name c2
.1. Folder Name d
.2. Folder Name e
.2. Folder Name e2
.3. Folder Name f '''
You could break it into lines:
content = content.split('\n')
Now it is list of lines, but these lines have spaces in front and end, which we need to remove:
content = [i.strip(' ') for i in content]
Now is time to tokenize it
def tokenize(x):
n,name = x.split(' Folder Name ',1)
return (int(n.replace('.','')),name)
content = [tokenize(i) for i in content]
Now print(content) gives [(0, 'a'), (1, 'b'), (2, 'c'), (2, 'c2'), (1, 'd'), (2, 'e'), (2, 'e2'), (3, 'f')] that is list of tuples (int,str).
Finally we could do traversal of tree:
import os
paths = []
while(content):
path = [content[0]]
for i in content[1:]:
if(i[0]<=path[-1][0]):
break
path.append(i)
paths.append(os.path.join(*[i[1] for i in path]))
content.remove(path[-1])
Now my paths is ['a/b/c', 'a/b/c2', 'a/b', 'a/d/e', 'a/d/e2/f', 'a/d/e2', 'a/d', 'a'] as I use Linux OS and os.path.join will use proper slashes for your system.
Explanation of above code: I traverse this tree and when I go into deadend I add path which lead to it to my list paths and remove that leaf (list remove always removes only 1 element so no need to worry about deleting same named folder being another leaf). I end when all leafs are deleted (hope that you could understand, sadly I am unable to explain it in more clear way)
As paths is now list of paths I could simply use os.makedirs() function. Note that if you would try to make dir which already exist it would result in error, so we need to check firstly if it does not exist:
for i in paths:
if not os.path.exists(i):
os.makedirs(i)
os.mkdirhas that information. If folderais/home/a, thenbwould be/home/a/b, and it is created at the desired level.