我正在尝试从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'
答案 0 :(得分:1)