Google-Colab没有这样的文件或目录

时间:2019-07-12 15:13:53

标签: python-3.x google-colaboratory

我正在尝试从YouTube教程中直接从Google Colab中的计算机访问数据集,并且不断收到错误消息,指出文件或目录不存在。我尝试用不同的方式编写路径名,使用'\'代替'/',使用r“ ...”,从教程中复制确切的代码,然后直接从属性复制路径,但没有任何效果。我在SO上发现了类似的问题,但无济于事。这是我的代码:

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

DATADIR = "C:/Datasets/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

这些是我的错误:

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-15-f8d293e3d38a> 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: '/Datasets/PetImages/Dog'

1 个答案:

答案 0 :(得分:1)

您必须先上传文件。

from google.colab import files

uploaded = files.upload()

有多种加载外部数据的方法。请检查此link