| 123456789101112131415161718192021222324252627282930313233343536 |
- import json
- import os
- from typing import Any
- from config.settings import USER_SETTING_PATH
- def update_user_setting(field_name: str, new_value: Any) -> bool:
- """
- 修改 json 文件中指定字段的值
- :param field_name: 要修改的字段名
- :param new_value: 新值
- :return: 执行成功返回 True,失败返回 False
- """
- try:
- with open(USER_SETTING_PATH, "r", encoding="utf-8") as f:
- data = json.load(f)
- data[field_name] = new_value
- with open(USER_SETTING_PATH, "w", encoding="utf-8") as f:
- json.dump(data, f, ensure_ascii=False, indent=2)
- return True
- except Exception as e:
- print(f"修改失败:{e}")
- return False
- def get_user_setting(field_name: str) -> Any:
- """读取指定字段值"""
- if not os.path.exists(USER_SETTING_PATH):
- return None
- with open(USER_SETTING_PATH, "r", encoding="utf-8") as f:
- data = json.load(f)
- return data.get(field_name)
|