title: Using os.walk() to recursively traverse directories in Python
body: <p>I want to navigate from the root directory to all other directories within and print the same.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>#!/usr/bin/python
  
  import os
  import fnmatch
  
  for root, dir, files in os.walk("."):
          print root
          print ""
          for items in fnmatch.filter(files, "*"):
                  print "..." + items
          print ""
  </code></pre>
  
  <p>And here's my O/P:</p>
  
  <pre><code>.
  
  ...Python_Notes
  ...pypy.py
  ...pypy.py.save
  ...classdemo.py
  ....goutputstream-J9ZUXW
  ...latest.py
  ...pack.py
  ...classdemo.pyc
  ...Python_Notes~
  ...module-demo.py
  ...filetype.py
  
  ./packagedemo
  
  ...classdemo.py
  ...__init__.pyc
  ...__init__.py
  ...classdemo.pyc
  </code></pre>
  
  <p>Above, <code>.</code> and <code>./packagedemo</code> are directories.</p>
  
  <p>However, I need to print the O/P in the following manner: </p>
  
  <pre><code>A
  ---a.txt
  ---b.txt
  ---B
  ------c.out
  </code></pre>
  
  <p>Above, <code>A</code> and <code>B</code> are directories and the rest are files.</p>