Skip to content

Instantly share code, notes, and snippets.

@revotu
Created August 9, 2017 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save revotu/93d6508a2755513337f1252cd3a1b62c to your computer and use it in GitHub Desktop.
Save revotu/93d6508a2755513337f1252cd3a1b62c to your computer and use it in GitHub Desktop.
Python Recursively Remove Empty Directories
#Recursively Remove Empty Directories
import os
for root, dirs, files in os.walk(path, topdown=False):
if not files and not dirs:
os.rmdir(root)
#Recursively Remove Empty Directories, During do something like os.remove(file)
import os
for root, dirs, files in os.walk(path, topdown=False):
# do something like os.remove(file)
if not os.listdir(root):
os.rmdir(root)
@revotu
Copy link
Author

revotu commented Aug 9, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment