Spaces:
Sleeping
Sleeping
File size: 730 Bytes
ed00004 |
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 |
import json
from pathlib import Path
from typing import Union
def json_load(json_pth: Union[Path, str]):
if not isinstance(json_pth, str):
json_pth = str(json_pth)
with open(json_pth) as f:
data = json.load(f)
return data
def json_dump(data, json_pth: Union[Path, str]):
if not isinstance(json_pth, str):
json_pth = str(json_pth)
with open(json_pth, "w") as f:
json.dump(data, f, indent=2)
def read_txt(txt_pth: Union[Path, str]) -> list:
with open(txt_pth) as f:
lines = f.read().split("\n")
return lines[:-1]
def write_txt(data, txt_pth: Union[Path, str]):
with open(txt_pth, "w") as f:
for item in data:
f.write("%s\n" % item)
|