fansub-utils/remove_empty_directories.py
LightArrowsEXE c6ab4b134b remove_empty_directories.py: v1.0.2
Make it only print deletions, not retained directories
2020-06-17 03:24:06 +02:00

53 lines
1.3 KiB
Python

#! /usr/bin/env python
"""
Removes all empty directories located in the current directory.
Huge parts were taken from a comment on this gist;
https://gist.github.com/jacobtomlinson/9031697#gistcomment-3130652
As well as this stackoverflow post:
https://stackoverflow.com/a/40347279
"""
import argparse
import glob
import os
__author__ = 'LightArrowsEXE'
__license__ = 'MIT'
__version__ = '1.0.2'
def fast_scandir(dirname):
subfolders = [f.path for f in os.scandir(dirname) if f.is_dir()]
for dirname in list(subfolders):
subfolders.extend(fast_scandir(dirname))
return subfolders
def main():
L = sorted(
fast_scandir(os.getcwd()),
key=lambda p: len(str(p)),
reverse=True,
)
for pdir in L:
try: # remove directory if empty
os.rmdir(pdir)
print(f"[-] Deleted \"{pdir}\"")
except OSError: # catch and continue if non-empty
continue
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-R", "--recursive",
action="store_false", default=True,
help="Encode files recursively (default: %(default)s)")
parser.parse_args()
args = parser.parse_args()
main()
input("\nDone (Press \"Enter\" to close this window)")