Skip to content

Instantly share code, notes, and snippets.

@brentvollebregt
Created April 11, 2017 02:51
Show Gist options
  • Save brentvollebregt/04ea53f3761667b4ff39e6f2caf5a5d9 to your computer and use it in GitHub Desktop.
Save brentvollebregt/04ea53f3761667b4ff39e6f2caf5a5d9 to your computer and use it in GitHub Desktop.
Remove all empty folder in a given directory
import os
import errno
folder_to_scan_and_delete = "C:/Users/Owner/Desktop/Folder Simulation/" # Remember '/' not '\', also make sure to end with '/'
show_ignored = True
show_deleted = True
deleted = 0
for root, dirs, files in os.walk(folder_to_scan_and_delete, topdown=False):
for name in dirs:
direcotry = os.path.join(root, name)
try:
os.rmdir(direcotry)
except OSError as ex:
if ex.errno == errno.ENOTEMPTY:
if show_ignored:
print ("[Ignored] : " + direcotry)
else:
if show_deleted:
print ("[Deleted] : " + direcotry)
deleted += 1
print ("Deleted: " + str(deleted))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment