Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
81 lines (65 loc) · 2.16 KB
/
Copy pathutils.py
File metadata and controls
81 lines (65 loc) · 2.16 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
76
77
78
79
80
81
import datetime
import json
import logging
import os
from os.path import exists
import numpy as np
logger = logging.getLogger(__name__)
def to_json(data_dict, path):
"""Save dictionary as JSON."""
def converter(obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
elif isinstance(obj, datetime.datetime):
return obj.__str__()
with open(path, 'w') as file:
logger.info(f'Save to: {path}')
json.dump(data_dict, file, default=converter)
def open_json(path):
"""Read JSON file."""
try:
with open(path, 'r') as file:
data_json = json.load(file)
return data_json
except:
logger.error(f'Cannot open {path}')
def check_file_existence(path, raise_error = False):
"""
Check file existence with given path
"""
bool_result = exists(path)
if bool_result:
if raise_error:
raise Exception("File " + str(path) + " has already existed!")
else:
print("File " + str(path) + " has already existed!")
return bool_result
def create_dir(path):
"""Create directory if the input path is not found."""
if not os.path.exists(path):
logger.info(f'Creating directory: {path}')
os.makedirs(path)
def get_csv_line_from_list(line_list):
"""
Create a comma seperated line without new line character with given list
"""
line_str = ""
for value in line_list:
line_str += str(value)
line_str += ","
return line_str
def create_empty_result_csv_file(path_save, first_line_list):
if not check_file_existence(path_save):
line = get_csv_line_from_list(first_line_list)
with open(path_save, "a") as f:
f.write(line)
f.write("\n")
def append_result_to_csv_file(path_save, line_list):
line = get_csv_line_from_list(line_list)
with open(path_save, "a") as f:
f.write(line)
f.write("\n")