【问题标题】:{Errno 2} No such file or directory (in Google Collab){Errno 2} 没有这样的文件或目录(在 Google Colab 中)
【发布时间】:2020-01-31 02:42:34
【问题描述】:

我需要通过提供网络来测试模型(在 Google Collab 上),所有 .png 文件都存在于位置 "/content/DataSet/Test Images"

为此,我需要:

  • 访问文件夹中的每张图片
  • 调整它们的大小
  • 将它们转换为 numPy 数组

这是我尝试过的:

    for filename in os.listdir(test_img_dir):
      if filename.endswith(".png"):
        im = image.imread(filename)
        #Followed By Resizing and conversion into NumPy array

但这就是我得到的:

    FileNotFoundError: [Errno 2] No such file or directory: 'lg566 (400).png'

我可以验证该图像确实存在。为什么会发生这种情况,我应该如何处理?

我添加了一个能重现问题HERE的笔记本

【问题讨论】:

  • 请分享一个能够重现问题的独立笔记本。重要的部分将是观察您将目录 DataSet 传输到后端的方式和位置。
  • @BobSmith 我已经做到了。请看一看。

标签: python-3.x numpy google-colaboratory


【解决方案1】:

您面临的问题是os.listdir() 将为您提供您正在使用的目录中的文件列表,而没有它们的相对路径。这为您提供了多种选择。

选项 A:

添加文件路径,即:

for filename in os.listdir(path):
  if filename.endswith(".png"):
    im = image.imread(path+"/"+filename)
    ...

选项 B:全局

from glob import glob

for filename in glob(path+"/*.png"):
  im = image.imread(filename)
  ...

【讨论】:

    猜你喜欢
    • 2019-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 2015-11-19
    • 2013-03-21
    相关资源
    最近更新 更多