import yaml import logging from pathlib import Path def load_config(config_filename: str = 'config.yml'): """ 加载位于项目根目录的配置文件。 :param config_filename: 配置文件的名称。 :return: 加载后的配置字典,如果文件未找到则返回空字典。 """ try: # Path(__file__) -> 获取当前文件 (utils.py) 的路径 # .resolve() -> 将其转换为绝对路径 project_root = Path(__file__).resolve().parent.parent.parent.parent print(project_root) # 将项目根目录和配置文件名拼接成完整路径 config_path = project_root / config_filename logging.info(f"正在从以下路径加载配置: {config_path}") with open(config_path, 'r', encoding='utf-8') as f: config = yaml.safe_load(f) if config is None: return {} return config except FileNotFoundError: logging.error(f"配置文件在预期的路径未找到: {config_path}") return {} except Exception as e: logging.error(f"加载配置文件时发生错误: {e}") return {}