If you have a file or a set of files that you want to access as data in Google Colab, then you can try one of the below options.
Option 1: Using the Upload File Script
from google.colab import files
files_uploaded = files.upload()
Once you add this to one of the cells of your Notebook, you will see "choose files" option, when you click on it, you will be able to upload files from your local (Windows/Linux/Mac) PC.
• data.txt(text/plain) - 18 bytes, last modified: n/a - 100% done
Saving data.txt to data.txt
Once the file(s) is uploaded, you will see it as logs and can be accessed using filename.
Example of reading the just uploaded local file.
with open("data.txt", "r") as file:
content = file.read()
print(content)
Option 2: Access Files from Google Drive
One other way is to upload your local files to Google Drive and access them by mounting them using the below code.
from google.colab import drive
drive.mount('/content/drive')
Facing issues? Have Questions? Post them here! I am happy to answer!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
- How to upload and read csv file in Google Colab
- Resolving DNS_PROBE_FINISHED_NXDOMAIN Google Chrome Error
- Fix - ModuleNotFoundError: No module named google.colab
- Fetch More then 10 Links Per Page in Google Search Result
- Find Nearest Gas Station using Google Map App on your Phone
- How to get nest aware on iPhone
- Fix: Cannot contact reCAPTCHA. Check your connection and try again.
- Enable Dark Mode in Google Search
- How to Mount Google Drive in Colab Notebook
- [Fix] reCAPTCHA not working in Web Browser
- Google Celebrates Rosa Bonheur 200th birthday with a Google Doodle
- How to Subscribe to Google Nest Aware on iPhone
- Google Local Guide Program and Perks of Contributing to Google Maps
- How to remove disable google search blue triangle
- 86 Gmail keyboard shortcuts that you may find Advantageous
- How to insert image into Google Sheets cell
- How to Open a New Notebook in Google Colab
- Google Colab: How to read file from Google Drive
- How to Access Local Files in Google Colab
- Installing Google Cloud macOS SDK
- Turning Off Chat in Gmail
- How to do a Reverse Image Search Using Google Tools
- Google Search Console: HTTPS is invalid and might prevent it from being indexed
- How to Show Line Numbers in Google Colab Cells
- Fetch as Google Crawl Error or Redirected Status
- Fix MySQL - Error: (OperationalError) (HY000) [08S01] Communications link failure - MySQL
- What is ValueError: math domain error and how to fix it - Python
- Android: Save Data in local Db using Android Room - Android
- Disable Startup Sound on macOS - MacOS
- How to Auto Click Mouse on Mac every X Seconds - MacOS
- How to Save a Notepad file as Microsoft Excel file - Microsoft
- How to delete SharePoint List Item programmatically using C#.Net - SharePoint
- Run Scripting Language (JavaScript, Python, Groovy, Ruby) in Java - Java