How to read files inside a folder in Drive inside a notebook

a 夏天 提交于 2019-12-10 11:59:02

问题


I want to read the dataset from drive inside Google Colab Notbook. how can I do that? I did something like this but it's not working.

train_data_dir = "data/train/"
validation_data_dir = "data/validation/"

The data folder is like this:

data/
    train/
        dogs/
            dog001.jpg
            dog002.jpg
            ...
        cats/
            cat001.jpg
            cat002.jpg
            ...
    validation/
        dogs/
            dog001.jpg
            dog002.jpg
            ...
        cats/
            cat001.jpg
            cat002.jpg
            ...

回答1:


I found a solution using the Drive FUSE wrapper

you have to install the drive fuse wrapper then make a directory then mount it, that directory is the root directory of your drive, upload your dataset to your google drive and access it using "/content/name_of_your_directory/dataset_name/"

in my case I followed these steps:

1-Install a Drive FUSE wrapper.

!apt-get update -qq 2>&1 > /dev/null
!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse

then

2-Generate auth tokens for Colab

from google.colab import auth
auth.authenticate_user()

3-Generate creds for the Drive FUSE library.

from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass

4-Work around misordering of STREAM and STDIN in Jupyter.

prompt = !google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass(prompt[0] + '\n\nEnter verification code: ')
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}

5-Create a directory and mount Google Drive using that directory.

!mkdir -p yourdirectory
!google-drive-ocamlfuse yourdirectory

6- access this directory from code like in my case

train_data_dir = "/content/yourdirectory/data/train/"
validation_data_dir = "/content/yourdirectory/data/validation/"


来源:https://stackoverflow.com/questions/50606118/how-to-read-files-inside-a-folder-in-drive-inside-a-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!