Machine Learning

Import or Upload Local File to Google Colab

Google Colab is a powerful tool that allows you to run Python code in the cloud. This can be useful for a variety of tasks, including data analysis and machine learning. One of the lesser known features of Google Colab is that you can also import or upload files stored on your local drive. In this article, we will show you how to read a file from your local drive in Google Colab using a quick code sample.

There are a few reasons why you as a data scientist might need to learn how to read files from your local drive in Google Colab.

  • One reason is that you may not have access to the data you need in the cloud. For example, if you are working on a project that requires data that is not stored in the cloud, you can access it on your local drive.
  • Another reason is that using files from your local drive can be faster than using files from the cloud.
  • Finally, using files from your local drive can be easy to customize and manage.

Executing the code below will prompt to select a file from the local drive and upload.

import pandas as pd
import io
from google.colab import files
uploaded = files.upload()

Once done with the above, all you need to do is execute the following code. The “FileName.csv” should be the name of the file that you uploaded.

df = pd.read_csv(io.BytesIO(uploaded['FileName.csv']))

In order to test if the file is loaded appropriately, execute the following command:

df.head()

If you have any questions about how to use Google Colab or need help getting started, don’t hesitate to reach out. We’re here to help!

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

View Comments

  • Hello, thanks for your post, is there a way to upload files directly to a specific folder? after using 'uploaded = files.upload()', the files are in the root folder, as I want to upload many files I would like t have them for example in a folder 'Uploaded', is that possible?

Recent Posts

Retrieval Augmented Generation (RAG) & LLM: Examples

Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…

4 days ago

How to Setup MEAN App with LangChain.js

Hey there! As I venture into building agentic MEAN apps with LangChain.js, I wanted to…

1 week ago

Build AI Chatbots for SAAS Using LLMs, RAG, Multi-Agent Frameworks

Software-as-a-Service (SaaS) providers have long relied on traditional chatbot solutions like AWS Lex and Google…

2 weeks ago

Creating a RAG Application Using LangGraph: Example Code

Retrieval-Augmented Generation (RAG) is an innovative generative AI method that combines retrieval-based search with large…

3 weeks ago

Building a RAG Application with LangChain: Example Code

The combination of Retrieval-Augmented Generation (RAG) and powerful language models enables the development of sophisticated…

3 weeks ago

Building an OpenAI Chatbot with LangChain

Have you ever wondered how to use OpenAI APIs to create custom chatbots? With advancements…

3 weeks ago