Created
December 19, 2016 21:49
-
-
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
This file contains hidden or 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
#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