You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Click 8.1.3指定cache_dir后仍报文件不存在错误的求助

问题

我正在使用Click 8.1.3解析命令行参数,cli.py代码片段如下:

import os
import click

@click.option('--output_dir', default=os.curdir, help='当outdated为true时,选择缓存文件的输出目录')
@click.option('--cache_dir', default=os.curdir, help='当outdated为false时,选择缓存文件所在的目录')
def main(outdated, nodes_path, river_path, cur_sta, up_sta, cutoff, upstream, downstream, cache_dir, output_dir):
    # 业务逻辑代码
    ……

执行以下命令时出现错误:

PS C:\Users\UserName\Desktop\test_cache2> calcstream --nodes_path XXX --river_path YYY --cur_sta 0 --up_sta 10 --cache_dir C:\Users\UserName\Desktop\test-cache --outdated False

关键错误信息:

FileNotFoundError: [Errno 2] No such file or directory: '.\\source_project_points.csv'

程序始终在当前工作目录(C:\Users\UserName\Desktop\test_cache2)查找缓存文件,但该文件实际存在于指定的C:\Users\UserName\Desktop\test-cache目录中。

解决方法

  • 修复参数传递链路:从错误栈追踪可知,cache_dir参数在函数调用链中可能未正确传递。例如检查get_upstream_stations_graph调用get_upstream_stations、build_graph调用tie_outside_node等环节,确保cache_dir参数被完整传递到最终读取文件的tie_outside_node函数中,未被中途重置为默认的os.curdir。

  • 强制转换为绝对路径:在main函数开头立即将接收的目录参数转换为绝对路径,避免相对路径混淆:

def main(outdated, nodes_path, river_path, cur_sta, up_sta, cutoff, upstream, downstream, cache_dir, output_dir):
    cache_dir = os.path.abspath(cache_dir)
    output_dir = os.path.abspath(output_dir)
    # 后续业务逻辑
    ……
  • 检查命令行参数格式:Windows环境下传递路径时,建议用引号包裹路径(即使无空格),避免解析异常:
calcstream --nodes_path XXX --river_path YYY --cur_sta 0 --up_sta 10 --cache_dir "C:\Users\UserName\Desktop\test-cache" --outdated False

内容的提问来源于stack exchange,提问作者forestbat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 20:00:58