在 Colab 中从 GDrive 读取图像时出现 FileNotFound 错误
FileNotFound error while reading images from GDrive in Colab
我正在尝试操作存储在我的 Google Drive 中不同图像目录中的图像以进行深度学习。但是,我遇到了 FileNotFound
异常。
假设,我在 Google Colab 中的主目录是:
MainDirectory ='/content/drive/My Drive/Colab Notebooks/2020YearDeepLearning/Animals/PetImages/'
并且该文件夹中有两个目录,名为 Dog
和 Cat
。我可以通过 运行 以下代码验证这一点:
MainDirectory ='/content/drive/My Drive/Colab Notebooks/2020YearDeepLearning/Animals/PetImages/'
Categories =['Dog','Cat']
for category in Categories:
path = os.path.join(MainDirectory,category)
print(path)
打印目录:
/content/drive/My Drive/Colab Notebooks/2020YearDeepLearning/Animals/PetImages/Dog
/content/drive/My Drive/Colab Notebooks/2020YearDeepLearning/Animals/PetImages/Cat
我现在要做的是显示图像,为此我编写了以下代码:
MainDirectory = '/content/drive/My Drive/Colab Notebooks/2020YearDeepLearning/Animals/PetImages/'
Categories =['Dog','Cat']
for category in Categories:
path =os.path.join(MainDirectory,category)
print(path)
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()
但是这会导致以下错误:
FileNotFoundError: [Errno 2] No such file or directory: '/content/drive/My Drive/Colab Notebooks/2020YearDeepLearning/Animals/PetImages/Dog'
附加信息
我导入了以下库:
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
并使用以下命令安装我的驱动器:
from google.colab import drive
drive.mount('/content/drive')
错误很可能是由文件路径中的空格引起的。在 MainDirectory
文件路径之前添加 r
字符串文字前缀,以用作原始字符串。这是更正后的代码:
MainDirectory = r'/content/drive/My Drive/Colab Notebooks/2020YearDeepLearning/Animals/PetImages/'
我正在尝试操作存储在我的 Google Drive 中不同图像目录中的图像以进行深度学习。但是,我遇到了 FileNotFound
异常。
假设,我在 Google Colab 中的主目录是:
MainDirectory ='/content/drive/My Drive/Colab Notebooks/2020YearDeepLearning/Animals/PetImages/'
并且该文件夹中有两个目录,名为 Dog
和 Cat
。我可以通过 运行 以下代码验证这一点:
MainDirectory ='/content/drive/My Drive/Colab Notebooks/2020YearDeepLearning/Animals/PetImages/'
Categories =['Dog','Cat']
for category in Categories:
path = os.path.join(MainDirectory,category)
print(path)
打印目录:
/content/drive/My Drive/Colab Notebooks/2020YearDeepLearning/Animals/PetImages/Dog
/content/drive/My Drive/Colab Notebooks/2020YearDeepLearning/Animals/PetImages/Cat
我现在要做的是显示图像,为此我编写了以下代码:
MainDirectory = '/content/drive/My Drive/Colab Notebooks/2020YearDeepLearning/Animals/PetImages/'
Categories =['Dog','Cat']
for category in Categories:
path =os.path.join(MainDirectory,category)
print(path)
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()
但是这会导致以下错误:
FileNotFoundError: [Errno 2] No such file or directory: '/content/drive/My Drive/Colab Notebooks/2020YearDeepLearning/Animals/PetImages/Dog'
附加信息
我导入了以下库:
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
并使用以下命令安装我的驱动器:
from google.colab import drive
drive.mount('/content/drive')
错误很可能是由文件路径中的空格引起的。在 MainDirectory
文件路径之前添加 r
字符串文字前缀,以用作原始字符串。这是更正后的代码:
MainDirectory = r'/content/drive/My Drive/Colab Notebooks/2020YearDeepLearning/Animals/PetImages/'