Skip to content

Instantly share code, notes, and snippets.

@noxan
Created November 15, 2012 15:43
Show Gist options
  • Save noxan/4079263 to your computer and use it in GitHub Desktop.
Save noxan/4079263 to your computer and use it in GitHub Desktop.
remove empty folders
import os
def removeEmptyFolders(path):
if not os.path.isdir(path):
return
# walk through folders
files = os.listdir(path)
if len(files):
for f in files:
fullpath = os.path.join(path, f)
if os.path.isdir(fullpath):
removeEmptyFolders(fullpath)
# delete empty folders
if len(files) == 0:
print "Remove empty folder: ", path
os.rmdir(path)
if __name__ == "__main__":
removeEmptyFolders('/home/noxan/Music/')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment