Created
April 11, 2017 02:51
-
-
Save brentvollebregt/04ea53f3761667b4ff39e6f2caf5a5d9 to your computer and use it in GitHub Desktop.
Remove all empty folder in a given directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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