37  Google Drive Integrations

Google Colab notebooks can easily integrate with other Google products and services

In this notebook we will interface with Google Drive to programmatically read and write files.

37.1 Mounting the Drive

We will first need to “mount” the Google Drive to the Colab filesystem, so we can access Drive files within Colab. When we mount the drive, we choose the name of a local subdirectory within the Colab filesystem (for example, “content/drive”) in which we would like to access the files:

from google.colab import drive

drive.mount('/content/drive')
Mounted at /content/drive

This process asks you to authorize the notebook to access your Google Drive.

Screenshot of login screen that asks if its ok to “Permit this notebook to access your Google Drive files?”

37.2 Accessing Files

After the drive has been mounted, now any files in your Google Drive are accessable to the notebook.

You just need to note the path to the file.

Take for example, this data file called “daily-prices-nflx.csv” which has been uploaded into the top level of the author’s Google Drive:

import os

csv_filepath = "/content/drive/MyDrive/daily-prices-nflx.csv"

# verifying the file exists at the specified path:
print(os.path.isfile(csv_filepath))
True

Reading the CSV file using pandas:

from pandas import read_csv

df = read_csv(csv_filepath)
df.head()
timestamp open high low close adjusted_close volume dividend_amount split_coefficient
0 2024-06-17 669.11 682.7099 665.1101 675.83 675.83 3631184 0.0 1.0
1 2024-06-14 655.05 675.5800 652.6400 669.38 669.38 4447116 0.0 1.0
2 2024-06-13 644.00 655.2500 642.3500 653.26 653.26 1863587 0.0 1.0
3 2024-06-12 652.21 655.7800 643.1100 650.06 650.06 2094381 0.0 1.0
4 2024-06-11 640.72 650.1900 640.5200 648.55 648.55 2167417 0.0 1.0

See the “Applied Data Science in Python” book for more information about working with this tabular data structure (i.e. a pandas DataFrame object).