In this part you will learn how to make your first steps with Google Colaboratory (or Colab in short).
Table of contents
First you should login to Google Colaboratory (Colab in short): https://colab.research.google.com
According to information from the Colaboratory main page, to mount your Google Drive you have to execute this code first:
1 2 |
from google.colab import drive drive.mount('/content/drive') |
After granted required permissions:
you will see message:
1 |
Mounted at /content/drive |
and after a while you should see your drive mounted in file tree on the left:
Your colab file tree should reflect your google drive
If you start new notebook:
you have to remember to mount your drive again. In the following example you create a file readme.txt
in a /content/drive/MyDrive/ul/test/
folder with the content:
1 |
readme |
and then you read its content back:
- Step 1: mount Google Drive
12from google.colab import drivedrive.mount('/content/drive')
1Mounted at /content/drive - Step 2: save data
12345678import ospathComponents = ['/content','drive','MyDrive','ul','test']fileName = 'readme.txt'path = os.path.join(*pathComponents, fileName)print(path)with open(path, 'w') as f:f.write('readme')
Output:
1/content/drive/MyDrive/ul/test/readme.txt - Step 3: load data
123456789pathComponents = ['/content','drive','MyDrive','ul','test']fileName = 'readme.txt'path = os.path.join(*pathComponents, fileName)print(path)with open(path, 'r') as f:lines = f.readlines()for line in lines:print(line)
Output:
12/content/drive/MyDrive/ul/test/readme.txtreadme