title: building a directory structure using Python with recursion
body: <p>I am trying to build following structure using Python</p>
  
  <pre><code>├───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
  </code></pre>
  
  <p>Here's my code</p>
  
  <pre><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)
  </code></pre>
  
  <p>The above code creates a structure:</p>
  
  <pre><code>├───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
  </code></pre>
  
  <p>Logic and Explanation:</p>
  
  <p>Whenever I encounter <code>list</code>, I set <code>flag=False</code> so that I can go 1 step back for next iteration when value is <code>dict</code>.</p>
  
  <p>But unfortunately value of <code>flag</code> variable is not changing. I can't get my head around it why value is not changing it is always 1.</p>
  
  <p><em>Note: Kindly ignore if there's mismatch between folder name in Tree structure and code. It's just an example.</em></p>