2022-01-04 15:56:13 +08:00
|
|
|
import os
|
|
|
|
import json
|
|
|
|
|
2022-01-11 11:02:46 +08:00
|
|
|
from setting import APP_NAME
|
2022-01-04 15:56:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
def real_file_path(param):
|
|
|
|
"""
|
|
|
|
返回文件真实路径
|
|
|
|
避免因为文件路径问题带来的麻烦
|
|
|
|
Parameters:
|
|
|
|
param: 文件相对路径
|
|
|
|
Returns:
|
|
|
|
path: 文件真实路径
|
|
|
|
"""
|
|
|
|
abs_path = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
rel_path = abs_path[:abs_path.find(APP_NAME) + len(APP_NAME)]
|
|
|
|
path = os.path.abspath(rel_path + param)
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
|
def read_json_file(param):
|
|
|
|
"""
|
|
|
|
读取json文件
|
|
|
|
Parameters:
|
|
|
|
param: json文件相对路径
|
|
|
|
Returns:
|
|
|
|
result: json内容
|
|
|
|
"""
|
|
|
|
with open(real_file_path(param), "r", encoding='utf-8') as f:
|
|
|
|
result = json.load(f)
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def sub_dict(param1, param2):
|
|
|
|
"""
|
|
|
|
获取字典的子集
|
|
|
|
Parameters:
|
|
|
|
param1: 原字典
|
|
|
|
param2: 子集字段
|
|
|
|
Returns:
|
|
|
|
子集
|
|
|
|
"""
|
|
|
|
return dict((key, value) for key, value in param1.items() if key in param2)
|