-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathremove_files.py
More file actions
32 lines (23 loc) · 756 Bytes
/
remove_files.py
File metadata and controls
32 lines (23 loc) · 756 Bytes
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
"""
BASICS
Simplifies file removal
"""
import os
# Removes empty directories, recursive
# Does not delete the original directory itself
# Based on https://gist.github.com/jacobtomlinson/9031697
# original_path (str): the original location, at start, both must be similar
def remove_empty_dirs(path, original_path):
if not os.path.isdir(path):
return
# remove empty subfolders
files = os.listdir(path)
if len(files):
for f in files:
fullpath = os.path.join(path, f)
if os.path.isdir(fullpath):
remove_empty_dirs(fullpath, original_path)
# if folder empty, delete it
files = os.listdir(path)
if (len(files) == 0) and (path != original_path):
os.rmdir(path)