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

未引用文件't'却触发FileNotFoundError,请求技术协助排查

问题排查:FileNotFoundError: 找不到文件't'

你的代码本身没有直接使用't'路径,但报错说明传入extract_features函数的image_paths_train或image_paths_test列表中,存在无效元素't',循环遍历到这个元素时,Image.open()尝试打开它,自然会触发文件找不到的错误。

解决步骤:

  • 第一步:定位问题列表
    在调用extract_features前,添加打印代码检查路径列表内容:

    # 检查训练集路径
    print("训练集路径列表:", image_paths_train)
    # 检查测试集路径
    print("测试集路径列表:", image_paths_test)
    

    运行后就能明确看到哪个列表里混入了't'这个无效元素。

  • 第二步:修正路径列表
    根据路径列表的生成方式调整:

    • 如果是手动定义的列表:检查是否不小心输入了't',删除该元素或替换为正确的图片路径。
    • 如果是从文件/数据库读取路径:检查读取逻辑,比如是否在分割字符串时用了错误的分隔符,或者数据源本身包含无效的't'行,修正读取代码过滤无效内容。
  • 第三步:添加防御性校验(可选)
    在extract_features函数中加入路径存在性检查,避免单个无效路径导致整个程序崩溃:

    import os
    # Extract the features from the images
    def extract_features(image_paths):
        features = []
        for image_path in image_paths:
            if not os.path.exists(image_path):
                print(f"跳过无效路径:{image_path}")
                continue
            image = Image.open(image_path)
            image = image.resize((224, 224))
            image = np.array(image)
            image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
            image = vgg.predict(image)
            image = np.reshape(image, image.shape[1]*image.shape[2]*image.shape[3])
            features.append(image)
        return np.array(features)
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 07:43:19