How to Read a File from Drive in Google Colab

In this blog, discover how Google Colab, a favorite among data science and machine learning enthusiasts, seamlessly integrates with Google Drive for easy file access, enabling you to read files effortlessly in a cloud-based Python environment.

Google Colab is a popular platform for data science and machine learning enthusiasts. It provides a free cloud-based environment that allows users to write and run Python code, and it comes with pre-installed packages like NumPy, Pandas, and TensorFlow. One of the features that make Google Colab stand out is its integration with Google Drive, which allows users to store and access data files easily. In this blog post, we’ll explore how to read a file from Drive in Google Colab.

Step 1: Mounting Google Drive

The first step is to mount Google Drive in Google Colab. This allows you to access the files stored in your Drive account from within Colab. To mount your Drive account, run the following code:

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

This will prompt you to authenticate your Google account and grant permission to Colab to access your Drive files. Follow the prompts, and once you’re authenticated, your Drive account will be mounted to the /content/drive directory in Colab.

Step 2: Accessing the File

Once your Drive account is mounted, you can access the files stored in it using the file path. For example, if you have a file named data.csv stored in the root directory of your Drive account, you can access it using the following code:

colab-panel-navigation

import pandas as pd
dataset = pd.read_csv('/content/drive/MyDrive/data.csv')

In this example, we’re using the pandas package to read the CSV file into a DataFrame. The file path is /content/drive/MyDrive/iris.csv, where MyDrive is the name of the default folder that Google Drive creates for your account.

If your file is stored in a subdirectory of your Drive account, you can specify the full path to the file. For example, if you have a file named iris.csv stored in a folder named my_data, which is located in the root directory of your Drive account, you can access it using the following code:

import pandas as pd
dataset = pd.read_csv('/content/drive/MyDrive/my_data/iris.csv')

Step 3: Uploading the File

If your file is not already stored in your Drive account, you can upload it using the following code:

from google.colab import files
uploaded = files.upload()

This will prompt you to select the file you want to upload from your local machine. Once you’ve selected the file, it will be uploaded to the root directory of your Drive account.

You can then access the file using the file path, as described in Step 2.

Conclusion

In this blog post, we’ve explored how to read a file from Drive in Google Colab. By mounting your Drive account and specifying the file path, you can easily access the files stored in your Drive account from within Colab. This makes it easy to work with large datasets and collaborate with others on data science and machine learning projects.

Remember to keep your file paths consistent and use the correct file format when reading files from Drive in Colab. Happy coding!


About Saturn Cloud

Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Request a demo today to learn more.