FileNotFoundError:[错误2]没有这样的文件或目录| Mac + Google Colab

时间:2020-03-02 12:12:39

标签: python macos google-colaboratory

我正在Google Colab上的文件中工作,并使用以下代码:

import numpy as np
import matplotlib.pyplot as plt
import os
import cv2

DATADIR="/Users/boris/PetImages"
CATEGORIES= ["Dog","Cat"]

for category in CATEGORIES:
  path = os.path.join(DATADIR, category) 
  for img in os.listdir(path):
    img_array = cv2.imread(os.path.join(path,img), cv2.IMREAD_GRAYSCALE)
    plt.imshow(img_array, cmap="gray")
    plt.show()
    break
break

我正在尝试在Mac上导入两个文件夹。但是,我继续收到以下错误:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-36-4ea265030377> in <module>()
      9 for category in CATEGORIES:
     10   path = os.path.join(DATADIR, category)
---> 11   for img in os.listdir(path):
     12     img_array = cv2.imread(os.path.join(path,img), cv2.IMREAD_GRAYSCALE)
     13     plt.imshow(img_array, cmap="gray")

FileNotFoundError: [Errno 2] No such file or directory: "/Users/boris/PetImages/Dog"

1 个答案:

答案 0 :(得分:1)

Google colab在无法访问本地文件系统的远程虚拟机上运行。要使用这些文件,您首先必须将它们上传到Colab机器。

有关在Colab中访问数据的各种方法的信息,请参见https://colab.research.google.com/notebooks/io.ipynb