Google colab changing directory

我只是一个虾纸丫 提交于 2019-12-06 07:14:00

Did you create a /drive directory? (By default, one does not exist. You'll need to mount your Drive using a recipe like this one.

Otherwise, create a directory first. For example,

!mkdir demo
%cd demo
!pwd

will show /content/demo.

First, you have to install OCaml Fuse to use this feature on Google Colab, run the following command to install OCaml Fuse :

!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
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}

During the installation, (usually) Collab will ask you to insert code verification twice, just follow the link and accept the permission, then copy the code and paste to the form field that appear.

After the installation finish, run the below :

!mkdir drive
!google-drive-ocamlfuse drive
!ls -lla drive/"Colab Notebooks"

and see the output, and then you can refer to your file on your drive like these :

x = "drive/Colab Notebooks/blablablah/blah/file.p"
y = "drive/Colab Notebooks/blablablah/blah/file.p"
z = "drive/Colab Notebooks/blablablah/blah/file.p"

hope this can help you!

If you want to change directory from google colab into google drive, connect with google drive first.

(note: it will require an email verification)

from google.colab import drive 
drive.mount('/content/gdrive')

Change to the google drive directory.

%cd /content/gdrive/My\ Drive/

to verify use this.

!pwd

Your current directory is /content/gdrive/My Drive/

%cd /content/drive/My\ Drive/your-location

!pwd

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