Windows and applications create many blank folders on your hard drive. And they stick even after you uninstall the software. To solve this issue of ghost folders, I created a simple python script to delete all empty folders in any given drive or folder path. Here is the code;


#-------------------------------------------------------------
#    rmblanks.py
#      Deletes all empty folders under a given path.
#    http://metinsaylan.com
#-------------------------------------------------------------

#    Usage: rmblanks.py "E:/Test"

import sys, os

if len(sys.argv) == 1:
    # Print usage
    print("Usage: rmblanks.py \"E:/TestFolder\"")
else:
    for root, dirs, files in os.walk(sys.argv[1], topdown=False):
        for name in dirs:
            try:
                if len(os.listdir( os.path.join(root, name) )) == 0: #check whether the directory is empty
                    print( "Deleting", os.path.join(root, name) )
                    try:
                        os.rmdir( os.path.join(root, name) )
                    except:
                        print( "FAILED :", os.path.join(root, name) )
                        pass
            except:
                pass

RmBlanks.py Usage

For this script to work, of course you need python to be installed on your computer.

Usage of the script is very simple:

  1. Open up a terminal/command prompt and navigate to the folder containing rmblanks.py script.
  2. Type rmblanks.py E:\Directory\Name\ToBe\Cleared
  3. Change E:\Directory\Name\ToBe\Cleared to any folder you want to clear.
  4. Depending on hard drive speed and levels of directories, this script may take some time to complete.

I hope you find it useful. Enjoy!