Google Colab: How to read file from Google Drive

In order to read a file that you have uploaded on your Google Drive in Google Colab, you can make use of the drive module from the google.colab package.

Step 1: Mount Google Drive using drive module

The first step is to mount your drive in a Notebook code cell.

from google.colab import drive

Once you execute this code you will see a message:

Permit this notebook to access your Google Drive files?

This notebook is requesting access to your Google Drive files. Granting access to Google Drive will permit code executed in the notebook to modify files in your Google Drive.
Make sure to review notebook code prior to allowing this access.

Connect to Google Drive

Permit this notebook to access your Google Drive files

You will be asked to login to your Gmail account and authorize the access permissions to Colab.

Step 2: Copy path of your file

Next you can go to the file icon on the left sidebar in Google Drive and expand drive -> MyDrive and copy path of your file (csv, txt, xlsx or any other)

drive_csv_file_path = '/content/drive/MyDrive/data/data.csv'
Colab copy file path from drive folder

Step 3: All good to use it in your Python Code!

import csv

def read_from_csv_file(data_file):
    with open(data_file, 'r') as file:
        csv_reader = csv.reader(file)
        for row in csv_reader:

drive_csv_file_path = '/content/drive/MyDrive/data/data.csv'


Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail:

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap