Skip to content

Commit 2fac859

Browse files
committed
Add file reader utility
1 parent ef621fb commit 2fac859

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

utils/__init__.py

Whitespace-only changes.

utils/file_reader.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import json
2+
from pathlib import Path
3+
4+
BASE_PATH = Path.cwd().joinpath("data")
5+
6+
7+
def read_file(file_name):
8+
file_path = get_file_with_json_ext(file_name)
9+
with open(file_path, mode="r", encoding="utf-8") as f:
10+
return json.load(f)
11+
12+
13+
def get_file_with_json_ext(file_name):
14+
return (
15+
BASE_PATH.joinpath(file_name)
16+
if ".json" in file_name
17+
else BASE_PATH.joinpath(f"{file_name}.json")
18+
)

0 commit comments

Comments
 (0)