How to Read Files from Google Drive in Google Colab

Ro ro
3 min readJul 24, 2024

--

Hi Guys!

Here, I’ll share how to set up Google Colab and access files stored on your Google Drive, step by step.

Background

I am just starting my Python and AI/ML journey. I have been launching Jupyter Notebooks through the Anaconda Navigator. While it is convenient because you don’t have to re-upload any files and can directly access files from your local host, backup becomes a concern. More importantly, it has been using quite a bit of my CPU, slowing down my device and causing lag. Therefore, I’ve decided to move to Google Colab! This way, I can store my files on Google Drive and not worry about backups or saving documents.

Anaconda Navigator Interface
Jupyter Notebook Interface

Step 1: Open Google Colab

Go to Google Colab and create a new notebook.

Directly create new Google Colab Files from your Google Drive Folder

Step 2: Mount Google Drive

You need to mount your Google Drive to access the files stored there. Add and run the following code cell to do this:

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

Step 3: Locate your folder & Copy path

After mounting, you will see the folder on the left-hand side. Navigate to your folder (Case1linear) in the left-hand bar. The first part of the path will generally be /content/drive/MyDrive. Right-click on the folder and select "Copy path".

Click on the folder symbol (circled) on the left-hand bar

Note: If you have many folders, look for the folder named content,not drive like the screenshot.

Click on content to get the dropdown of drive & MyDrive

Step 4: Load your data

Use np.load to load your files from the specified path. Add and run the following code cell:

import numpy as np 

# Define the path to your files - rmb to include the backslash at the end if it is not with the file name
path = '/content/drive/'

# Load the data
X = np.load(path + 'X.npy')
y = np.load(path + 'Y.npy')

Hope this helped! If you found this article useful, please give it a clap and follow me for more tips and tutorials. Feel free to leave any questions, suggestions, or comments below. Happy coding!

--

--