title: Creating a loop to print html lists
body: <p>I need to create a html list similar in form to the following:</p>
  
  <pre><code>&lt;ul class="treeView"&gt;
      &lt;li&gt;
          Root Item
          &lt;ul class="collapsibleList"&gt;
          &lt;li&gt;
              Sub-Folder 1
              &lt;ul&gt;
                  &lt;li&gt;
                      Sub-Sub-Folder 1
                      &lt;ul&gt;
                          &lt;li&gt;Item 1&lt;/li&gt;
                          &lt;li class="lastChild"&gt;Item 2&lt;/li&gt;
                      &lt;/ul&gt;
                  &lt;/li&gt;
                  &lt;li class="lastChild"&gt;
                      Sub-Sub-Folder 2
                      &lt;ul&gt;
                          &lt;li&gt;Item 3&lt;/li&gt;
                          &lt;li class="lastChild"&gt;Item 4&lt;/li&gt;
                      &lt;/ul&gt;
                  &lt;/li&gt;
              &lt;/ul&gt;
          &lt;/li&gt;
              &lt;li class="lastChild"&gt;
                  Sub-Folder 2
                  &lt;ul&gt;
                      &lt;li&gt;Item 5&lt;/li&gt;
                      &lt;li&gt;Item 6&lt;/li&gt;
                      &lt;li class="lastChild"&gt;Item 7&lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/li&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>This list is based off a directory structure, but I have the code down for listing the actual structure. So all I need is how to use the for loop to keep track of everything and create the nested html lists.</p>
  
  <p>Below is my python script that I have so far:</p>
  
  <pre><code>for item in lstItems:
      splitfile = os.path.split(item[0])
      webpyPath = splitfile[0].replace("/srv/http/example/www", "")
      itemName = splitfile[1]
      if item[1] == 0:
          lstRemDir.append(itemName)
      intDirNum = len(lstRemDir) - 1
      if item[1] == 0:
          if len(lstRemDir) == 1:
              print '''
  &lt;ul class="treeView"&gt;
      &lt;li&gt;Collapsible lists
          &lt;ul class="collapsibleList"&gt;
              '''
          elif len(lstRemDir) != 1:
              f.write('    &lt;/ul&gt;\n&lt;/li&gt;')
          f.write('&lt;li&gt;&lt;a href=\"' + webpyPath + "/" + itemName + '\" id=\"directory\" alt=\"' + itemName + '\" target=\"viewer\"&gt;' + itemName + '&lt;/a&gt;&lt;/li&gt;\n')
      elif item[1] == 1:
          f.write('&lt;li&gt;&lt;a href=\"' + webpyPath + "/" + itemName + '\" id=\"file\" alt=\"' + itemName + '\" target=\"viewer\"&gt;' + itemName + '&lt;/a&gt;&lt;/li&gt;\n')
      else:
          f.write('&lt;li&gt;An error happened in processing ' + itemName + '.')
  </code></pre>
  
  <p>The reason I am asking is because I have been trying to get this figured out for days. I am not asking for someone to do it for me, just a good way to do it. I have tried using lists to store data but I felt like it got too confusing. Hopefully that clarifies what I am looking for.</p>
  
  <p>Thanks in advance!</p>