1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright (c) 2021 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import os 17import json 18 19 20def read_json_file(input_file): 21 if not os.path.exists(input_file): 22 print("file '{}' doesn't exist.".format(input_file)) 23 return None 24 25 data = None 26 try: 27 with open(input_file, 'r') as input_f: 28 data = json.load(input_f) 29 except json.decoder.JSONDecodeError: 30 print("The file '{}' format is incorrect.".format(input_file)) 31 raise 32 return data 33 34 35def write_json_file(output_file, content): 36 file_dir = os.path.dirname(os.path.abspath(output_file)) 37 if not os.path.exists(file_dir): 38 os.makedirs(file_dir, exist_ok=True) 39 with open(output_file, 'w') as output_f: 40 json.dump(content, output_f, indent=2) 41