How to Mount Google Drive on Google Colab

Google Colab allows you to easily access and manipulate files stored in your Google Drive directly within your Colab notebooks. Here's a step-by-step guide on how to mount your Google Drive on Google Colab:

Step 1: Open Google Colab

Go to Google Colab and sign in with your Google account if you haven't already.

Step 2: Create a New Notebook or Open an Existing One

You can either create a new notebook by clicking on "File" > "New Notebook" or open an existing one by uploading it from your local system or opening it from Google Drive.

Step 3: Mount Google Drive

In a code cell within your Colab notebook, run the following Python code:

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

Step 4: Authenticate and Authorize

When you run the code cell, a link will appear. Click on the link and follow the instructions to authorize Google Colab to access your Google Drive. After authorization, you'll receive an authentication code. Copy the code and paste it into the prompt in your Colab notebook.

Step 5: Access Your Google Drive

Once you've successfully mounted your Google Drive, you can access it like any other directory in your Colab notebook. Your Google Drive will be mounted at /content/drive/.

Example Usage

Here's an example of how you can use your mounted Google Drive in a Colab notebook:

import pandas as pd
df = pd.read_csv('/content/drive/My Drive/data.csv')
print(df.head())

Conclusion

Mounting your Google Drive on Google Colab provides a convenient way to access and work with your files directly within your Colab notebooks. By following the steps outlined above, you can seamlessly integrate your Google Drive with your data analysis, machine learning, or other projects in Google Colab.

Comments

Archive

Contact Form

Send