title: Autogenerate Markdown index from a recursive folder with makdown files in python
body: <p>Hi I have a folder with many subfolders with markdown (.md) files and I want to generate an index for this. I try this</p>
  
  <pre><code>MD_FILE_LIST = (y for x in os.walk(DOC_FILES_DIR)
                  for y in glob(os.path.join(x[0], '*.md')))
  
  with open(DOC_FILES_DIR + 'index.md', 'w') as f:
      text = []
      for m_file in MD_FILE_LIST:
          text.append('%s[%s](/%s)\n' % (m_file.count(os.sep)*' ',
                                    m_file.split(os.sep)[-1].replace('.md', ''),
                                    m_file.replace(DOC_FILES_DIR, '').replace(os.sep,'/')))
  
      f.writelines(text)
  </code></pre>
  
  <p>But the generated file did´t show a linked html view, it's shown as a plain text <code>[name](url)</code>. as I shown in the picture
  And also if it's possible create a heading at least for the 2 or 3 first levels</p>
  
  <p><a href="https://i.stack.imgur.com/oEuBN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oEuBN.png" alt="enter image description here"></a></p>
  
  <p><strong>NEW version</strong></p>
  
  <p>I made some fix so now looks like <a href="https://github.com/efirvida/OpenFoam-Documentation" rel="nofollow noreferrer">THIS</a> here is my the updated version code:</p>
  
  <pre><code>with open(DOC_FILES_DIR + 'README.md', 'w') as f:
      text = []
      for m_file in MD_FILE_LIST:
          levels = m_file.replace(DOC_FILES_DIR, '').count(os.sep)
          if levels &lt; 3:
              text.append( ' * ' + '#' * levels + ' [%s](./%s)\n' % (
                                      m_file.split(os.sep)[-1].replace('.md', ''),
                                      m_file.replace(DOC_FILES_DIR, '').replace(os.sep,'/')))
          else:
              text.append('%s[%s](./%s)\n' % (m_file.count(os.sep)*' ' + '- ',
                          m_file.split(os.sep)[-1].replace('.md', ''),
                          m_file.replace(DOC_FILES_DIR, '').replace(os.sep, '/')))
      text[0] = "# Index\n"
      f.writelines(text)
  </code></pre>
  
  <p>but seems that I can't use headings and list at the same time </p>