I am trying to build following structure using Python
├───Baseframework
│ ├───0_src
│ │ ├───Appfun
│ │ │ ├───Cpugeneric
│ │ │ ├───i5t
│ │ │ ├───Tricore
│ │ │ └───vpprint
│ │ └───Base
│ │ ├───gnfru
│ │ ├───ills
│ │ └───service
│ ├───1_toolbar
│ │ └───0_build
│ │ ├───0_utilites
│ │ ├───1_config
│ │ └───9_make
│ └───2_out
│ ├───tasking
│ └───tricore
Here's my code
import os
import argparse
base_path = "c:/usr"
# level_zero = "Baseframework"
levels = {"Baseframework":{"0_src":{"Appfun":["Cpugeneric", "i5t", "Tricore", "vpprint"], "Base":["ills","gnfru","service"]},
"1_toolbar":{"0_build":["0_utilities", "1_config", "9_make"]},
"2_out": ["ist_tasking","tricore_gyne"]}}
def iterdict(d, bd, flag = 1):
for k,v in d.items():
if isinstance(v, dict):
print(flag)
if flag==1:
bd = os.path.join(bd,k)
print(bd)
else:
bd = os.path.join(os.path.abspath(bd+"/.."),k)
print(bd)
if not os.path.exists(bd):
os.mkdir(bd)
iterdict(v, bd)
elif isinstance(v, list):
bd1 = os.path.join(bd, k)
if not os.path.exists(bd1):
os.mkdir(bd1)
for i in v:
os.mkdir(os.path.join(bd1, i))
flag = 0
iterdict(levels, base_path)
The above code creates a structure:
├───Baseframework
│ └───0_src
│ ├───1_toolbar
│ │ ├───0_build
│ │ │ ├───0_utilities
│ │ │ ├───1_config
│ │ │ └───9_make
│ │ └───2_out
│ │ ├───ist_tasking
│ │ └───tricore_gyne
│ ├───Appfun
│ │ ├───Cpugeneric
│ │ ├───i5t
│ │ ├───Tricore
│ │ └───vpprint
│ └───Base
│ ├───gnfru
│ ├───ills
│ └───service
Logic and Explanation:
Whenever I encounter list, I set flag=False so that I can go 1 step back for next iteration when value is dict.
But unfortunately value of flag variable is not changing. I can't get my head around it why value is not changing it is always 1.
Note: Kindly ignore if there's mismatch between folder name in Tree structure and code. It's just an example.