agudmund

Remove empty directories on Windows

Dec 25th, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. #!/bin/env python
  2. import os
  3. import sys
  4.  
  5. # Windows Empty directory cleaner
  6. # Drag and drop a root folder on top of this script to remove all empty directories within the hierarchy there below.
  7. # Also accepts the root level directory as a trailing command line argument.
  8.  
  9. confirm = False
  10. path = r'%s' % sys.argv[ -1 ]
  11.  
  12. def rm( folder ):
  13.     """Removes a folder"""
  14.  
  15.     try:
  16.         os.rmdir( r'%s' % folder )
  17.         print '--[ Deleted %s' % folder
  18.         return True
  19.     except:
  20.         return False
  21.  
  22. for folder,subfolder,files in os.walk( path ):
  23.     if ( subfolder == [] and files == [] ):
  24.         if not confirm:
  25.             answer = raw_input( '--[Confirm] Remove: %s ? (yes/no/all)' % folder )
  26.  
  27.             if answer in ( 'all', 'All', 'ALL', 'a' ):
  28.                 confirm = True
  29.                 rm( folder )
  30.             elif answer in ( 'yes', 'YES', 'Yes', 'y'):
  31.                 rm( folder )
  32.             else:
  33.                 continue
  34.         else:
  35.             rm( folder )
  36.  
  37. raw_input( '--[Done] Press any key to continue' )
Advertisement
Add Comment
Please, Sign In to add comment