-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
62 lines (57 loc) · 1.94 KB
/
utils.py
File metadata and controls
62 lines (57 loc) · 1.94 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
import os
def create_folder(folder_name:str):
"""
Create a folder with the given name.
if the folder already exists, append a number to the name.
a folder with the same name and number might already exist.
in that case, increment the number until a unique name is found.
"""
counter = 1
new_folder_name = folder_name
while os.path.exists(new_folder_name):
new_folder_name = f"{folder_name}_{counter}"
counter += 1
os.makedirs(new_folder_name)
return new_folder_name
def create_file(file_name:str):
"""
Create a file with the given name.
if the file already exists, append a number to the name.
a file with the same name and number might already exist.
in that case, increment the number until a unique name is found.
"""
base_name, ext = os.path.splitext(file_name)
counter = 1
new_file_name = file_name
while os.path.exists(new_file_name):
new_file_name = f"{base_name}_{counter}{ext}"
counter += 1
with open(new_file_name, 'w') as f:
pass # Create an empty file
return new_file_name
log_file = ""
def init_log(input_file:str):
"""
Initialize the log file by creating it if it doesn't exist.
"""
global log_file
log_file = os.path.splitext(input_file)[0] + ".log"
log_file = create_file(log_file)
with open(log_file, 'w') as f:
f.write("Log file initialized.\n")
print(f"Log file created: {log_file}")
def log(section:str, message:str, verbose=False, mandatory=False):
"""
Print a message to the console, formatted with the section name.
Prints if verbose is True or if the message is mandatory.
"""
if verbose:
save_log(log_file, f"{section}: {message}")
if mandatory:
print(f"{section}: {message}")
def save_log(log_file:str, message:str):
"""
Save a message to the log file.
"""
with open(log_file, 'a') as f:
f.write(message + '\n')