Mounting Google Drive to Google Colaboratory

Saurish Srivastava

January 07, 2021

image of Google Colab

Everyone's faced it before. We add files to our local runtime and create our machine learning models, data analysis pipelines, or simply just execute code in Python through Google Colab. And we're happy! But then our runtime is disconnected and all our files are gone, poof! But there's a way we can eliminate this problem: by mounting our Google Drive to Google Colab.

The Code

# Load the drive helper and mount
from google.colab import drive

# This will prompt for authorization; follow the steps!
drive.mount('/content/drive', force_remount=True)

What next?

It's important to note that this will mount your drive at the location /content/drive – you still need to utilize %cd to change directories and navigate around your drive. Using %ls will give you an idea of the contents inside your drive and what folders you need to access.