[colab into the pit series 1] Google drive authorization and working path

Thanks to google, thanks to capitalist wool, for providing us with such a good learning tool. There are relatively few online documents, and there are some pits in the use of colab. I recorded them for future reference. Maybe it can help children's shoes who encounter the same problem~

Google drive authorization

It seems that after a period of time, the environment allocated by colab will be automatically initialized, and the next time you come in, you will have to remount google drive. I created a new notebook to store the authorization process. Every time the environment is affected, I just need to open the notebook and execute the following again. Execute the following code, there will be two prompts in the middle, asking you to open the authorization address and fill in the drive authorization code. The authorization authority for the two times is different, so it must be filled in.

  !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}

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

switch working path

In the colab environment, the location where we mount Google drive is /content/drive/ . The notebook and py files in colab use /content/ as the working directory by default. You need to execute the following command to manually switch the working directory, for example:

import os

path = "/content/drive/colab-notebook/lesson1-week2/assignment2"
os.chdir(path)
os.listdir(path)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324456281&siteId=291194637