-
Notifications
You must be signed in to change notification settings - Fork 53
/
clean_all.py
87 lines (65 loc) · 2.12 KB
/
clean_all.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""Scripts deleting Biogeme output .py
:author: Michel Bierlaire
:date: Sun Jul 30 15:58:57 2023
"""
import os
import sys
FILE_ENDING_WITH = ['pickle', 'iter', 'log', 'pareto', '~', '#']
FILE_STARTING_WITH = ['Untitled', 'test_dumped']
def file_to_erase(the_filename: str) -> bool:
"""Checks if a file must be erased
:param the_filename: name fo the file
:type the_filename: str
:return: True if the file must be erased. False otherwise.
:rtype: bool
"""
for end in FILE_ENDING_WITH:
if the_filename.endswith(end):
return True
for start in FILE_STARTING_WITH:
if the_filename.startswith(start):
return True
return False
def get_file_list():
"""Retrieves the list of .py to be erased.
:return: list of file names
:rtype: list[str]
"""
the_list = []
current_directory = os.getcwd()
for dirpath, _, filenames in os.walk(current_directory):
for filename in filenames:
if file_to_erase(filename):
file_path = os.path.join(dirpath, filename)
the_list.append(file_path)
return the_list
def ask_for_confirmation(the_list):
"""Function asking confirmation to the user.
:param the_list: luist of .py to erase
:type the_list: list[str]
:return: True if OK to proceed, False otherwise.
:rtype: bool
"""
while True:
print('This script will erase the following .py:')
print('\n'.join(the_list))
text = 'Do you want to proceed? (yes/no): '
response = input(text).strip().lower()
if response in ('yes', 'y'):
return True
if response in ('no', 'n'):
return False
print("Invalid response. Please enter 'yes' or 'no'.")
file_list = get_file_list()
if not file_list:
print('No file to be cleaned.')
sys.exit(0)
if ask_for_confirmation(file_list):
for file_name in file_list:
os.remove(file_name)
if len(file_list) == 1:
print('One file has been erased.')
else:
print(f'{len(file_list)} .py have been erased.')
else:
print("Script execution aborted.")