如何将文件(或csv)从本地驱动器读入google colab(而不是从Google驱动器读入)
我认为这会起作用:
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
from tqdm import tqdm
DATADIR = "F:/Colab Notebooks/kagglecatsanddogs_3367a/PetImages"
CATEGORIES = ["Dog", "Cat"]
for category in CATEGORIES: # do dogs and cats
path = os.path.join(DATADIR,category) # create path to dogs and cats
for img in os.listdir(path): # iterate over each image per dogs and cats
img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE) # convert to array
plt.imshow(img_array, cmap='gray') # graph it
plt.show() # display!
但是我得到这个错误
FileNotFoundError: [Errno 2] No such file or directory: 'F:/Colab Notebooks/kagglecatsanddogs_3367a/Dog'
编辑:
当我在本地计算机而不是Google Colab上运行该代码时,以上代码可以工作
答案 0 :(得分:1)
Colab在具有自己的驱动器和目录的云计算机上运行。 对于一个或几个会话,您可以简单地上传所需的文件。点击屏幕最左侧的小箭头,转到文件并上传所需的任何内容。
如果您不想每次都上传文件,可以将其保存在Google云端硬盘中,并按以下方式访问它:
from google.colab import drive
drive.mount('/content/drive', force_remount=True)
with open("/content/drive/My Drive/...", 'r') as f:
data = ... (read the file here)