-
Hussain Kanafani authoredHussain Kanafani authored
utils.py 781 B
import re
import json
import os
def read_json(file_path):
try:
with open(file_path, 'r') as f:
try:
return json.load(f)
except ValueError:
raise ValueError('{} is not valid'.format(file_path))
except IOError:
raise IOError('{} does not exist.'.format(file_path))
def write_json(obj, file_path):
make_directory(os.path.dirname(file_path))
with open(file_path, 'w') as f:
json.dump(obj, f)
def digits_in_string(text):
non_digit = re.compile("\D")
return int(non_digit.sub("", text))
def make_directory(directory):
if not os.path.exists(directory):
try:
os.makedirs(directory)
except OSError as err:
raise
return directory