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

Windows环境下读取指定目录触发FileNotFoundError错误求助

解决Keras flow_from_directory的FileNotFoundError问题

问题场景

本地文件夹路径为C:\Pet_Classification\data\train,使用以下Keras代码读取目录:

train_data_dir = 'C:/Pet_Classification/data/train'

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

运行后抛出FileNotFoundError,错误栈如下:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-42-536baa9a7d03> in <module>
      3     target_size=(img_width, img_height),
      4     batch_size=batch_size,
----> 5     class_mode='binary')

/usr/local/lib/python3.7/site-packages/keras/preprocessing/image.py in flow_from_directory(self, directory, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, save_to_dir, save_prefix, save_format, follow_links, subset, interpolation)
    990         follow_links=follow_links,
    991         subset=subset,
--> 992         interpolation=interpolation)
    993 
    994   def flow_from_dataframe(self,

/usr/local/lib/python3.7/site-packages/keras/preprocessing/image.py in __init__(self, directory, image_data_generator, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, data_format, save_to_dir, save_prefix, save_format, follow_links, subset, interpolation, dtype)
    408         subset=subset,
    409         interpolation=interpolation,
--> 410         **kwargs)
    411 
    412 

/usr/local/lib/python3.7/site-packages/keras_preprocessing/image/directory_iterator.py in __init__(self, directory, image_data_generator, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, data_format, save_to_dir, save_prefix, save_format, follow_links, subset, interpolation, dtype)
    113         if not classes:
    114             classes = []
--> 115             for subdir in sorted(os.listdir(directory)):
    116                 if os.path.isdir(os.path.join(directory, subdir)):
    117                     classes.append(subdir)

FileNotFoundError: [Errno 2] No such file or directory: 'C:/Pet_Classification/data/train'

排查与解决步骤

1. 验证路径真实性

  • 打开文件资源管理器,直接粘贴C:\Pet_Classification\data\train回车,确认目录是否存在。若不存在,需创建对应目录或修正路径拼写(注意大小写、空格、特殊字符)。

2. 调整路径格式(避免转义问题)

Windows系统中Python路径推荐两种写法,可任选:

  • 原始字符串(最直观):
    train_data_dir = r'C:\Pet_Classification\data\train'
    
  • 双反斜杠:
    train_data_dir = 'C:\\Pet_Classification\\data\\train'
    

(你当前用的正斜杠写法本身合法,但若路径含转义字符会失效,优先推荐前两种)

3. 检查目录结构是否符合Keras要求

flow_from_directory要求目标目录下必须包含分类子目录,结构示例:

train/
    cat/
        img1.jpg
        img2.jpg
    dog/
        img3.jpg
        img4.jpg

若train目录下直接存放图片,无分类子目录,也会触发读取失败(本质是无法识别有效类别目录)。

4. 排查目录权限

右键C:\Pet_Classification\data\train→属性→安全,确认当前运行Python的用户拥有“读取和执行”权限,避免因权限不足导致无法访问目录。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 22:20:35