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;

#      Deletes all empty folders under a given path.

#    Usage: "E:/Test"

import sys, os

if len(sys.argv) == 1:
    # Print usage
    print("Usage: \"E:/TestFolder\"")
    for root, dirs, files in os.walk(sys.argv[1], topdown=False):
        for name in dirs:
                if len(os.listdir( os.path.join(root, name) )) == 0: #check whether the directory is empty
                    print( "Deleting", os.path.join(root, name) )
                        os.rmdir( os.path.join(root, name) )
                        print( "FAILED :", os.path.join(root, name) )
                pass 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 script.
  2. Type 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!

Join The Discussion (2 Responses)