Skip to content

Instantly share code, notes, and snippets.

@fakuivan
Created December 19, 2016 21:49
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 fakuivan/83314a9e6ea7d37f81e06bb3a52d204d to your computer and use it in GitHub Desktop.
Save fakuivan/83314a9e6ea7d37f81e06bb3a52d204d to your computer and use it in GitHub Desktop.
A simple script that deletes all of those annoing empty folders on your backups
#Empty folder deleter: A simple script that deletes all of those annoing empty folders on your backups
import os
from pathlib import Path
import argparse
def delete_empty_folders(path):
no_empty_dirs = True
for root, dirs, files in os.walk(str(path), topdown=False):
if os.listdir(root) == []:
print('Deleting direcotry: "{}"'.format(root))
no_empty_dirs = False
os.rmdir(root)
if no_empty_dirs:
print('Directory "{}" already clean!'.format(path))
def main():
parser = argparse.ArgumentParser(description='Delete empty folders nested on a root directory.')
parser.add_argument('root_dir', metavar='P', type=Path, help='The root directory where to search for empty directories')
args = parser.parse_args()
root_dir = args.root_dir
if root_dir.exists() == False:
print('Directory "{}" does not exist'.format(root_dir), file=os.sys.stderr)
os.sys.exit(1)
print('Let\'s clean "{}"'.format(root_dir))
delete_empty_folders(root_dir)
if __name__ == "__main__": main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment