title: Deleting folders in python recursively
body: <p>I'm having a problem with deleting empty directories. Here is my code:</p>
  <pre><code>for dirpath, dirnames, filenames in os.walk(dir_to_search):
      //other codes
  
      try:
          os.rmdir(dirpath)
      except OSError as ex:
          print(ex)
  </code></pre>
  <p>The argument <code>dir_to_search</code> is where I'm passing the directory where the work needs to be done. That directory looks like this:</p>
  <pre><code>test/20/...
  test/22/...
  test/25/...
  test/26/...
  </code></pre>
  <p>Note that all the above folders are empty. When I run this script the folders <code>20</code>,<code>25</code> alone gets deleted! But the folders <code>25</code> and <code>26</code> aren't deleted, even though they are empty folders.</p>
  <h3>Edit:</h3>
  <p>The exception that I'm getting are:</p>
  <pre><code>[Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test'
  [Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test/2012'
  [Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test/2012/10'
  [Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test/2012/10/29'
  [Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test/2012/10/29/tmp'
  [Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test/2012/10/28'
  [Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test/2012/10/28/tmp'
  [Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test/2012/10/26'
  [Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test/2012/10/25'
  [Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test/2012/10/27'
  [Errno 39] Directory not empty: '/home/python-user/shell-scripts/s3logs/test/2012/10/27/tmp'
  </code></pre>
  <p>Where am I making a mistake?</p>