From 25b010c004bbce2acab0ac6bd803d6d19db9efbb Mon Sep 17 00:00:00 2001 From: zhiyucn Date: Sun, 1 Jun 2025 19:12:56 +0800 Subject: [PATCH] =?UTF-8?q?fix(heartFC=5FCycleinfo):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E5=88=9B=E5=BB=BA=E6=97=B6=E7=89=B9=E6=AE=8A?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E5=A4=84=E7=90=86=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 移除目录创建时的异常处理逻辑,改为在创建前过滤特殊字符。仅保留字母、数字、下划线、中划线和中文字符,确保目录路径安全。简化了错误处理流程,直接使用os.makedirs的exist_ok参数避免竞态条件。 --- src/chat/focus_chat/heartFC_Cycleinfo.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/chat/focus_chat/heartFC_Cycleinfo.py b/src/chat/focus_chat/heartFC_Cycleinfo.py index 989461089..1fb932035 100644 --- a/src/chat/focus_chat/heartFC_Cycleinfo.py +++ b/src/chat/focus_chat/heartFC_Cycleinfo.py @@ -97,13 +97,12 @@ class CycleDetail: """将循环信息写入文件""" # 如果目录不存在,则创建目录 dir_name = os.path.dirname(file_path) + # 去除特殊字符,保留字母、数字、下划线、中划线和中文 + dir_name = "".join( + char for char in dir_name if char.isalnum() or char in ["_", "-", "/"] or "\u4e00" <= char <= "\u9fff" + ) if dir_name and not os.path.exists(dir_name): - try: - os.makedirs(dir_name, exist_ok=True) - except Exception as e: - print(f"创建目录 {dir_name} 失败: {e}") - print(f"日志将会写入到temp目录下") - file_path = os.path.join("temp", os.path.basename(file_path)) + os.makedirs(dir_name, exist_ok=True) # 写入文件 import json