簡體   English   中英

從本地驅動器讀取文件到google colab

[英]read in a file from a local drive to google colab

如何將文件(或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上運行代碼時,以上代碼可以正常工作

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM