Created
September 13, 2020 15:09
-
-
Save ggorlen/ffa402f61cf69e7a862c4d9e9b5a9579 to your computer and use it in GitHub Desktop.
remove empty folders recursively
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
""" | |
removes all empty folders recursively in a directory | |
""" | |
import os | |
import sys | |
def delete_if_empty(path): | |
if not os.path.isdir(path): | |
return False | |
elif not os.listdir(path) or delete_empty_folders_recursively(path): | |
print("deleted: " + path) | |
os.rmdir(path) | |
return True | |
def delete_empty_folders_recursively(directory="."): | |
return all([delete_if_empty(os.path.join(directory, f)) for f in os.listdir(directory)]) | |
if __name__ == "__main__": | |
delete_empty_folders_recursively(sys.argv[1] if len(sys.argv) > 1 else ".") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment