How to access Google Drive from Google Colab notebook?

Dec 26, 2025

Leave a message

In the realm of data science, machine learning, and programming, Google Colab has emerged as a powerful and widely used tool. It offers a cloud - based Jupyter notebook environment that requires no setup and can be run entirely in the browser, providing access to free GPU and TPU resources. Often, you'll have data stored in Google Drive that you'd like to use within your Google Colab notebooks. Whether it's datasets for model training, pre - trained models, or even just note - taking files related to your projects, understanding how to access Google Drive from Google Colab is a crucial skill. As a notebook supplier, I understand the importance of having a seamless workflow and access to all necessary resources, which is why I'm excited to share this guide with you.

Setting up the Environment

Before we can access Google Drive from Google Colab, we need to ensure that everything is set up correctly. Launch a new or existing Google Colab notebook. Google Colab is accessible through your Google account, and you can find it in your Google Drive under the 'New' menu. Once inside the Colab notebook, you'll see a Python - based coding environment with cells where you can write and execute code.

Mounting Google Drive

The key step to accessing Google Drive is the process of mounting it. Mounting essentially means connecting the Google Colab environment to your Google Drive so that Colab can read and write files from/to it. To mount Google Drive, you'll need to run a specific block of code in a Colab cell.

Notebook ManufacturersNotebook manufacturer

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

When you run this code, Colab will prompt you to authorize access to your Google Drive. Click the link provided in the output, which will take you to a Google authentication page. Here, you'll need to select the Google account associated with the Drive you want to access and then allow Colab to access your Drive files. After granting permission, you'll receive an authorization code. Copy this code and paste it back into the input field in the Colab cell. Once you hit enter, Google Drive will be successfully mounted at the /content/drive directory within the Colab environment.

Navigating and Accessing Files

After mounting Google Drive, you can start exploring and accessing your files. You can use Python's os module to navigate through the directories in your Google Drive. For example, if you want to list the contents of your Google Drive's root directory, you can run the following code:

import os
os.listdir('/content/drive/MyDrive')

This will display a list of all files and folders in your Google Drive's main directory. If you want to access a specific file, say, a CSV dataset, you can use Python libraries like pandas to read it.

import pandas as pd
data = pd.read_csv('/content/drive/MyDrive/path/to/your/file.csv')

As a notebook supplier, I know that document and data handling are crucial in everyday work. We offer a variety of options for note - taking and documenting your work. Take a look at our Cork Covered Notebook, which not only provides a unique and stylish look but also a smooth writing experience for jotting down your important ideas and findings.

Working with Folders and Sub - folders

Google Drive allows you to organize your files into folders and sub - folders. To access files within a sub - folder, you simply need to specify the full path. For example, if you have a folder named 'Project Data' in your Google Drive, and within it, there's a sub - folder named 'Test Data' that contains a text file, you can access it like this:

file_path = '/content/drive/MyDrive/Project Data/Test Data/test_file.txt'
with open(file_path, 'r') as file:
    content = file.read()
    print(content)

This shows how you can access and read the contents of a text file stored in a specific sub - folder of your Google Drive.

Writing Files to Google Drive

Not only can you read files from Google Drive, but you can also write files to it. Suppose you've done some data processing in your Colab notebook and want to save the results. You can use the following code to save a text file:

new_file_path = '/content/drive/MyDrive/results.txt'
with open(new_file_path, 'w') as new_file:
    new_file.write('This is the result of my data processing.')


This will create a file named'results.txt' in the root of your Google Drive with the specified text. For those in the business of planning and scheduling, our Promotional Wall Calendars 2023 can be a great addition to your workspace, helping you keep track of important deadlines and events related to your Google Colab projects.

Handling Permissions and Errors

Sometimes, you may encounter issues related to permissions or errors while accessing Google Drive from Colab. If you get a 'permission denied' error, it could be because you haven't granted the necessary access rights. Double - check that you've authenticated correctly and that the account you're using has permission to access the files in question.

If you face connectivity issues, try unmounting and remounting Google Drive. You can unmount it using the following code:

from google.colab import drive
drive.flush_and_unmount()

And then remount it as described earlier.

Using Google Drive for Model Storage and Sharing

For those working on machine learning and deep learning projects, Google Drive can serve as a great place to store and share your models. Suppose you've trained a neural network model in Colab. You can save the model's weights to Google Drive and then use them later or share them with your team members.

import tensorflow as tf

# Assume model is your trained TensorFlow model
model.save('/content/drive/MyDrive/trained_model.h5')

After saving the model, you can easily access it from anywhere as long as you have access to your Google Drive. And for the creative and sleek note - taking needs, don't forget to check out our Fake Leather Notebook, perfect for scribbling down quick thoughts about your model architecture and hyperparameters.

Conclusion

Accessing Google Drive from Google Colab is an essential skill that enhances your productivity and expands the capabilities of your data - related projects. It provides a seamless way to manage and utilize your data, models, and other resources. As a notebook supplier, we recognize the importance of having the right tools for documentation and data management in your workflow, whether it's notebooks for taking down your results or calendars for project planning.

If you're interested in our wide range of notebooks and promotional products for your office or project needs, we'd love to hear from you. Contact us to discuss your specific requirements and start the procurement process. Working together, we can ensure that you have the best tools to complement your Google Colab - based projects.

References

  • Google Colab official documentation
  • Python official documentation
  • TensorFlow official documentation
  • Pandas official documentation

Send Inquiry