From 6d59e97130eb5001b24abb3e5d6ddb23163ded77 Mon Sep 17 00:00:00 2001 From: zhiyucn Date: Sun, 1 Jun 2025 19:04:24 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix(heartFC=5FCycleinfo):=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E7=9B=AE=E5=BD=95=E5=88=9B=E5=BB=BA=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E6=97=B6=E7=9A=84=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=E5=92=8C?= =?UTF-8?q?=E5=A4=87=E7=94=A8=E6=96=B9=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 当目录创建失败时,捕获异常并打印错误信息,同时将日志文件写入到temp目录下作为备用方案 --- src/chat/focus_chat/heartFC_Cycleinfo.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/chat/focus_chat/heartFC_Cycleinfo.py b/src/chat/focus_chat/heartFC_Cycleinfo.py index 129a58af8..989461089 100644 --- a/src/chat/focus_chat/heartFC_Cycleinfo.py +++ b/src/chat/focus_chat/heartFC_Cycleinfo.py @@ -98,7 +98,12 @@ class CycleDetail: # 如果目录不存在,则创建目录 dir_name = os.path.dirname(file_path) if dir_name and not os.path.exists(dir_name): - os.makedirs(dir_name, exist_ok=True) + 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)) # 写入文件 import json From 25b010c004bbce2acab0ac6bd803d6d19db9efbb Mon Sep 17 00:00:00 2001 From: zhiyucn Date: Sun, 1 Jun 2025 19:12:56 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix(heartFC=5FCycleinfo):=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E7=9B=AE=E5=BD=95=E5=88=9B=E5=BB=BA=E6=97=B6=E7=89=B9?= =?UTF-8?q?=E6=AE=8A=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 From d0f3747292e242b1b3ef5c14125a6648877c3ff0 Mon Sep 17 00:00:00 2001 From: zhiyucn Date: Sun, 1 Jun 2025 19:24:39 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix(heartFC=5FCycleinfo):=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E6=96=87=E4=BB=B6=E8=B7=AF=E5=BE=84=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=B9=B6=E6=B7=BB=E5=8A=A0=E8=B0=83=E8=AF=95=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复目录路径拼接问题,确保文件写入到正确路径 - 添加调试日志打印目录名和最终文件路径,便于问题排查 --- src/chat/focus_chat/heartFC_Cycleinfo.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/chat/focus_chat/heartFC_Cycleinfo.py b/src/chat/focus_chat/heartFC_Cycleinfo.py index 1fb932035..a12dc861f 100644 --- a/src/chat/focus_chat/heartFC_Cycleinfo.py +++ b/src/chat/focus_chat/heartFC_Cycleinfo.py @@ -95,17 +95,20 @@ class CycleDetail: def log_cycle_to_file(self, file_path: str): """将循环信息写入文件""" - # 如果目录不存在,则创建目录 + # 如果目录不存在,则创建目 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" ) + print("dir_name:", dir_name) if dir_name and not os.path.exists(dir_name): os.makedirs(dir_name, exist_ok=True) # 写入文件 import json + file_path = os.path.join(dir_name, os.path.basename(file_path)) + print("file_path:", file_path) with open(file_path, "a", encoding="utf-8") as f: f.write(json.dumps(self.to_dict(), ensure_ascii=False) + "\n")