title: Strange behavior of os.walk() in Python?
body: <p>This is the original block of code and its result:</p>
  
  <p>Code:</p>
  
  <pre><code>if os.path.isdir(top):
      for root, dirs, files in os.walk(top, topdown = True):
          for dirname in dirs:
              print 'Dirname = ', os.path.join(root, dirname)
  </code></pre>
  
  <p>Results:</p>
  
  <pre><code>Dirname = ../output/.svn
  Dirname = ../output/a random folder
  Dirname = ../output/a random folder - copy
  Dirname = ../output/.svn\pristine
  Dirname = ../output/.svn\temp
  Dirname = ../output/.svn\pristine\04
  Dirname = ../output/.svn\pristine\59
  Dirname = ../output/a random folder\another one inside
  Dirname = ../output/a random folder\another one inside - Copy
  Dirname = ../output/a random folder\another one inside - Copy (2)
  </code></pre>
  
  <p>Now I want to ignore all hidden folders and subfolders. This is the modified code and its result:</p>
  
  <p>Code:</p>
  
  <pre><code>if os.path.isdir(top):
      for root, dirs, files in os.walk(top, topdown = True):
          for dirname in dirs:
              print 'Dirname = ', os.path.join(root, dirname)
              if dirname.startswith('.'):
                  dirs.remove(dirname)
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>Dirname = ../output/.svn
  Dirname = ../output/a random folder - copy
  Dirname = ../output/a random folder\another one inside
  Dirname = ../output/a random folder\another one inside - Copy
  Dirname = ../output/a random folder\another one inside - Copy (2)
  </code></pre>
  
  <p>What I don't understand is: why is ../output/a random folder not listed anymore??</p>