如何从本地驱动器读取文件(或CSV)到Google Colab?报错咨询
Hey there! I see exactly what's going on here. Your code works perfectly on your local machine because your computer can directly access your F: drive—but Google Colab runs on a remote cloud virtual machine, which has no way to see or connect to your local hard drive. That’s why you’re hitting that FileNotFoundError when trying to use your local path in Colab.
Let’s walk through a couple of straightforward fixes to get your local PetImages folder working in Colab:
Option 1: Upload the Folder to Colab’s Temporary Storage (Great for Small/Medium Datasets)
Since Colab can’t reach your local drive, you’ll need to move the files to Colab’s cloud storage first:
- Zip up your
PetImagesfolder on your local computer (right-click > Send to > Compressed (zipped) folder). - In Colab, click the folder icon in the left sidebar to open the file explorer.
- Click the upload button (the upward arrow) and select your zipped
PetImagesfile. - Once uploaded, run this command in a Colab code cell to unzip it:
!unzip PetImages.zip -d /content/ - Update your
DATADIRpath in your code to point to the unzipped folder in Colab:DATADIR = "/content/PetImages" - Run your original code—this time it should find the files without issues!
Option 2: Mount Your Local Drive Directly (For Larger Datasets)
If your dataset is too big to zip/upload quickly, you can mount your local drive to Colab using Chrome’s file system access:
- Run this code in a Colab cell:
from google.colab import files # This will prompt you to grant Colab access to your local files files.mount('/content/local_drive') - Follow the on-screen prompts to allow Colab to access your local drive. Once mounted, you’ll find your local files under
/content/local_drive/MyDrive(adjust the path to match your system’s structure). - Update your
DATADIRto the mounted path of yourPetImagesfolder, like:DATADIR = "/content/local_drive/MyDrive/Colab Notebooks/kagglecatsanddogs_3367a/PetImages" - Now your code can read directly from your local drive through this mounted connection.
Quick Heads-Up:
Colab’s temporary storage resets when your session ends (if you close the tab or it times out), so you’ll need to re-upload files if you use Option 1 and start a new session. Option 2 avoids this since it’s directly accessing your local drive each time.
Here’s your modified code ready to use with Option 1 (plus a small fix for corrupted images common in the Kaggle cats/dogs dataset):
import numpy as np import matplotlib.pyplot as plt import os import cv2 from tqdm import tqdm DATADIR = "/content/PetImages" # Updated path for Colab CATEGORIES = ["Dog", "Cat"] for category in CATEGORIES: path = os.path.join(DATADIR, category) for img in os.listdir(path): try: # Skip corrupted images that might break your code img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE) plt.imshow(img_array, cmap='gray') plt.show() except Exception as e: print(f"Skipping corrupted image {img}: {e}")
内容的提问来源于stack exchange,提问作者Mr.Riply

