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:
2026-05-04 23:05:10 +08:00
parent 6fd14b4e49
commit 6f96bf50ac
18 changed files with 84 additions and 271 deletions
+20 -22
View File
@@ -35,11 +35,11 @@ def ensure_directories():
"""确保必要的目录结构存在"""
config = ConfigManager()
directories = [
config.get('Paths', 'input_folder', fallback='data/input'),
config.get('Paths', 'output_folder', fallback='data/output'),
'data/result',
config.get('Paths', 'temp_folder', fallback='data/temp'),
'logs'
config.get_path('Paths', 'input_folder', fallback='data/input', create=True),
config.get_path('Paths', 'output_folder', fallback='data/output', create=True),
config.get_path('Paths', 'result_folder', fallback='data/result', create=True),
config.get_path('Paths', 'temp_folder', fallback='data/temp', create=True),
os.path.join(config.app_root, 'logs')
]
for directory in directories:
if not os.path.exists(directory):
@@ -52,8 +52,8 @@ def clean_cache(log_widget):
from .command_runner import set_running_task
try:
config = ConfigManager()
processed_record = config.get('Paths', 'processed_record', fallback='data/processed_files.json')
output_folder = config.get('Paths', 'output_folder', fallback='data/output')
processed_record = config.get_path('Paths', 'processed_record', fallback='data/processed_files.json')
output_folder = config.get_path('Paths', 'output_folder', fallback='data/output')
cache_files = [
processed_record,
os.path.join(output_folder, "processed_files.json"),
@@ -65,7 +65,7 @@ def clean_cache(log_widget):
os.remove(cache_file)
add_to_log(log_widget, f"已清除缓存文件: {cache_file}\n", "success")
temp_dir = os.path.join("data/temp")
temp_dir = config.get_path('Paths', 'temp_folder', fallback='data/temp')
if os.path.exists(temp_dir):
for file in os.listdir(temp_dir):
file_path = os.path.join(temp_dir, file)
@@ -76,7 +76,7 @@ def clean_cache(log_widget):
except Exception as e:
add_to_log(log_widget, f"清除文件时出错: {file_path}, 错误: {str(e)}\n", "error")
log_dir = "logs"
log_dir = os.path.join(config.app_root, 'logs')
if os.path.exists(log_dir):
for file in os.listdir(log_dir):
if file.endswith(".active"):
@@ -98,22 +98,18 @@ def clean_cache(log_widget):
def open_result_directory():
try:
result_dir = os.path.abspath("data/result")
if not os.path.exists(result_dir):
os.makedirs(result_dir, exist_ok=True)
config = ConfigManager()
result_dir = config.get_path('Paths', 'result_folder', fallback='data/result', create=True)
os.startfile(result_dir)
except Exception as e:
messagebox.showerror("错误", f"无法打开结果目录: {str(e)}")
def _open_directory_from_settings(settings_key, default_path, label):
"""通用的从用户设置读取路径并打开目录"""
from .user_settings import load_user_settings
def _open_directory_from_settings(config_key, default_path, label):
"""通用的从置读取路径并打开目录"""
try:
s = load_user_settings()
path = os.path.abspath(s.get(settings_key, default_path))
if not os.path.exists(path):
os.makedirs(path, exist_ok=True)
config = ConfigManager()
path = config.get_path('Paths', config_key, fallback=default_path, create=True)
os.startfile(path)
except Exception as e:
messagebox.showerror("错误", f"无法打开{label}: {str(e)}")
@@ -138,9 +134,10 @@ def clean_data_files(log_widget):
add_to_log(log_widget, "操作已取消\n", "info")
return
config = ConfigManager()
files_cleaned = 0
input_dir = "data/input"
input_dir = config.get_path('Paths', 'input_folder', fallback='data/input')
if os.path.exists(input_dir):
for file in os.listdir(input_dir):
file_path = os.path.join(input_dir, file)
@@ -149,7 +146,7 @@ def clean_data_files(log_widget):
files_cleaned += 1
add_to_log(log_widget, "已清理input目录\n", "info")
output_dir = "data/output"
output_dir = config.get_path('Paths', 'output_folder', fallback='data/output')
if os.path.exists(output_dir):
for file in os.listdir(output_dir):
file_path = os.path.join(output_dir, file)
@@ -170,8 +167,9 @@ def clean_result_files(log_widget):
if not messagebox.askyesno("确认清理", "确定要清理result目录的文件吗?这将删除所有已生成的采购单文件。"):
add_to_log(log_widget, "操作已取消\n", "info")
return
config = ConfigManager()
count = 0
result_dir = "data/result"
result_dir = config.get_path('Paths', 'result_folder', fallback='data/result')
if os.path.exists(result_dir):
for file in os.listdir(result_dir):
file_path = os.path.join(result_dir, file)