36 lines
770 B
Python
Raw Normal View History

2018-07-02 10:17:14 +01:00
from __future__ import absolute_import
import os
import os.path as osp
import errno
import json
from collections import OrderedDict
2019-02-20 21:51:24 +00:00
import warnings
2018-07-02 10:17:14 +01:00
def mkdir_if_missing(directory):
if not osp.exists(directory):
try:
os.makedirs(directory)
except OSError as e:
if e.errno != errno.EEXIST:
raise
2018-08-07 17:14:28 +01:00
def check_isfile(path):
isfile = osp.isfile(path)
if not isfile:
2019-02-20 21:51:24 +00:00
warnings.warn('No file found at "{}"'.format(path))
2018-08-07 17:14:28 +01:00
return isfile
2018-07-02 10:17:14 +01:00
def read_json(fpath):
with open(fpath, 'r') as f:
obj = json.load(f)
return obj
def write_json(obj, fpath):
mkdir_if_missing(osp.dirname(fpath))
with open(fpath, 'w') as f:
2019-02-20 21:51:24 +00:00
json.dump(obj, f, indent=4, separators=(',', ': '))