-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmptyFolderCleaner.py
56 lines (47 loc) · 1.94 KB
/
EmptyFolderCleaner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
def get_subdirectories(path):
"""
Return a list containing the absolute path of the direct subdirectories in the provided path. Doesn't walk the
directory tree.
:param path: the path to check
:return: a list with the direct subdirectories names
"""
return [os.path.join(path, o) for o in os.listdir(path)
if os.path.isdir(os.path.join(path, o))]
def clean_empty_folders(path, removeRoot=True, verbose=False):
"""
Recursively remove empty folders and subdirectories.
:param path: the initial (root) path
:param removeRoot: if True (default), also removes the root path if all subdirectories are empty
:param verbose: if True, prints progress
:return: True if 'path' was removed, false otherwise. Note that, if removeRoot=False, this function will always
return False
"""
if not os.path.isdir(path):
if verbose:
print(path, "is not a directory. Abort.")
return False
subdirectories = get_subdirectories(path)
for subdirectory in subdirectories:
clean_empty_folders(subdirectory, removeRoot=True, verbose=verbose)
# After removing subdirectories, check if folder is empty
if not os.listdir(path) and removeRoot:
os.rmdir(path)
if verbose:
print(path, "removed")
return True
return False
if __name__ == '__main__':
selected_path = input("Write the desired path: ")
remove_root = None
while remove_root is None:
remove_root_input = input("Also remove root folder (Y/N)? ").lower()
if remove_root_input == "y":
remove_root = True
break
elif remove_root_input == "n":
remove_root = False
break
print(f"Invalid input '{remove_root_input}', please try again.")
clean_empty_folders(selected_path, removeRoot=remove_root, verbose=True)
print("Done!")