fix: 修复输出路径问题 — 路径解析改为基于应用目录而非CWD
当从外部目录(如D:\ccc)拖入文件时,输出文件会错误地写入源目录。 根因是所有路径使用相对路径 + os.getcwd() 解析,CWD不同则路径错误。 修复方案: - ConfigManager.get_path() 改为使用 app_root (exe所在目录/脚本所在目录) - 将 22 处裸硬编码 "data/result"/"data/output" 替换为 config.get_path() - 添加 result_folder 到默认配置和 config.ini - 修复 error_utils.py 中的路径匹配字符串 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -19,6 +19,7 @@ DEFAULT_CONFIG = {
|
||||
'Paths': {
|
||||
'input_folder': 'data/input',
|
||||
'output_folder': 'data/output',
|
||||
'result_folder': 'data/result',
|
||||
'temp_folder': 'data/temp',
|
||||
'template_folder': 'templates',
|
||||
'template_file': '银豹-采购单模板.xls',
|
||||
|
||||
+12
-3
@@ -33,7 +33,16 @@ class ConfigManager:
|
||||
|
||||
def _init(self, config_file):
|
||||
"""初始化配置管理器"""
|
||||
self.config_file = config_file or 'config.ini'
|
||||
# 计算应用根目录(不依赖 os.getcwd())
|
||||
import sys
|
||||
if getattr(sys, 'frozen', False):
|
||||
# PyInstaller 打包后,根目录是 exe 所在目录
|
||||
self.app_root = os.path.dirname(sys.executable)
|
||||
else:
|
||||
# 源码运行,根目录是 app/config/ 的上两级
|
||||
self.app_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
self.config_file = config_file or os.path.join(self.app_root, 'config.ini')
|
||||
self.config = configparser.ConfigParser()
|
||||
self.load_config()
|
||||
|
||||
@@ -153,8 +162,8 @@ class ConfigManager:
|
||||
path = Path(path_str)
|
||||
|
||||
if not path.is_absolute():
|
||||
# 相对路径,转为绝对路径(相对于项目根目录)
|
||||
path = Path(os.getcwd()) / path
|
||||
# 相对路径,转为绝对路径(相对于应用根目录)
|
||||
path = Path(self.app_root) / path
|
||||
|
||||
if create:
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user