Module 6 of 13 · Google Colab: Cloud Computing for AI · Beginner

Using Google Drive with Colab

Duration: 5 min

This module explores how to effectively integrate Google Drive with Google Colab, a powerful cloud computing platform for AI. Understanding this integration is crucial as it allows you to manage, access, and utilize your data stored in Google Drive directly within your Colab notebooks, enhancing your workflow efficiency and data management capabilities.

Mounting Google Drive in Colab

Mounting Google Drive in Colab enables you to access your files directly from your Colab environment. This is particularly useful for loading datasets, saving model outputs, or working with large files that are too cumbersome to upload directly to Colab. The process involves authenticating your Google account and mounting the Drive as a file system within Colab.

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

Try it in Google Colab: Open in Colab

Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth... 
Enter your authorization code:..............

Mounted at /content/drive

Accessing Files in Google Drive

Once Google Drive is mounted, you can access its contents just like a local file system. This section covers how to read from and write to files in your Drive, facilitating seamless data manipulation and model training processes. Understanding file paths and operations is key to efficiently managing your data.

import pandas as pd

# Reading a CSV file from Google Drive
file_path = '/content/drive/My Drive/data/my_data.csv'
df = pd.read_csv(file_path)

# Writing a DataFrame back to Google Drive
df.to_csv('/content/drive/My Drive/data/processed_data.csv', index=False)

💡 Tip: Ensure your file paths are correctly specified, including the full path from the root of your Drive to the file. Misplaced or incorrect paths can lead to file not found errors.

❓ What is the primary purpose of mounting Google Drive in Colab?

❓ How can you read a CSV file from your Google Drive in Colab?

← Previous Continue interactively → Next →

Related Courses