read_json.py 1.56 KB
Newer Older
mwx committed
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
# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test63
# FileName:      read_json.py
# Author:       lao_zhao
# Datetime:     2024/11/5 14:17
# Description:
# 
# ---------------------------------------------------------------------------
import json
import os

from APIAutoTest_v3_1 import log


def read_json(file_path):
    """读取json文件,将json文件的内容序列化为python对象在返回"""
    log.info(f"执行的函数为:read_json,描述为:读取json文件,将json文件的内容序列化为python对象在返回,传入的参数为:{file_path}")
    if isinstance(file_path, str) and os.path.isfile(file_path) and file_path.endswith(".json"):
        with open(file_path, mode="r", encoding="utf-8") as f:
            try:
                return json.loads(f.read())
            except Exception as e:
                log.error(
                    f"执行的函数为:read_json,描述为:读取json文件,将json文件的内容序列化为python对象在返回,传入的参数为:{file_path}, 文件路径不存在"
                    f"错误类型为:{type(e)}, 错误的描述为:{e}")
                raise e
    else:
        log.error(f"执行的函数为:read_json,描述为:读取json文件,将json文件的内容序列化为python对象在返回,传入的参数为:{file_path}, 文件路径不存在"
                  f"错误类型为:FileNotFoundError, 错误的描述为:文件路径错误")
        raise FileNotFoundError("文件路径错误")