Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google-Colab No such file or directory

I'm trying to access a dataset directly from my computer in Google Colab from a Youtube tutorial and I keep getting errors saying the file or directory doesn't exist. I've tried writing the path name differently, using '\' instead of '/', using r"...",copying the exact code from the tutorial and directly copying the path from properties and nothing works. I've found similar questions on SO but nothing that helps. Here is my code:

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

These are my errors:

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'
like image 512
Josh Carr Avatar asked Oct 15 '25 18:10

Josh Carr


2 Answers

You have to upload your files first.

from google.colab import files

uploaded = files.upload()

There are various ways to load the external data. Please check this link

like image 137
nag Avatar answered Oct 17 '25 22:10

nag


I was getting the same error.

I got the solution by mounting the google drive in colab and then running the

import pandas as pd
data = pd.read_csv(path, header=0)
data

And it worked well.

like image 23
AliakbarETH Avatar answered Oct 18 '25 00:10

AliakbarETH