[7c5f70]: / Utilities / remove_empty_results.py

Download this file

76 lines (48 with data), 1.6 kB

 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
from time import time
import shutil
def get_empty_folders(root_path, items_allowed=2):
# Folders to delete
del_folders = []
# Get a list of subdirs
walker = os.walk(root_path)
for w in walker:
# Separate base folder, subdirectories, and files
base = w[0]
subdirs = w[1]
files = w[2]
# Check length of files in folder
file_len = len(subdirs) == 0 and len(files) <= items_allowed
# Check modified date
file_dat = within_modify_date(base)
if file_len and not file_dat:
del_folders.append(base)
return del_folders
def within_modify_date(path, creation_safety=60):
"""
Compares the creation date of a folder or file with the current time.
Returns True if the folder/file has not been modified with the
creation_safety period.
Args:
path (str): path to file or folder
creation_safety (str): time in seconds, default 60 mins
Returns:
bool: True if the folder/file has been modified within the specified
time
"""
# The modification time
folder_time = os.path.getmtime(path)
# Compare with current time
output = (time() - folder_time) < creation_safety
return output
def delete_folders(folders):
for folder in folders:
# Remove folder
shutil.rmtree(folder)
print('Removed %s' % folder)
if __name__ == "__main__":
root_path = 'E:\\MR Data\\ML_Results'
# Get empty folder names
folders = get_empty_folders(root_path, items_allowed=5)
# Delete empty folders
delete_folders(folders)