-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
36 lines (25 loc) · 919 Bytes
/
utils.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
import json
import os
from pathlib import Path
from log import logger
def create_empty_json_file(filepath):
filepath = Path(filepath)
filepath.parent.mkdir(parents=True, exist_ok=True)
with open(filepath, 'w') as outfile:
json.dump({}, outfile)
def read_from_json_file(filepath) -> dict:
if not os.path.exists(filepath):
create_empty_json_file(filepath)
with open(filepath, "r") as outfile:
report_data = json.load(outfile)
logger.info("Read json successful")
return report_data
def write_to_json_file(filename, report_data):
with open(filename, "w") as outfile:
json.dump(report_data, outfile)
logger.info("Write to json successful")
def get_project_root_dir():
return os.path.dirname(os.path.abspath(__file__))
def get_filename_from_filepath(filepath: Path):
directory_name, filename = os.path.split(filepath)
return filename