In order to read a file that you have uploaded on your Google Drive in Google Colab, you can make use of the drive module from the google.colab package.
Step 1: Mount Google Drive using drive module
The first step is to mount your drive in a Notebook code cell.
from google.colab import drive
drive.mount('/content/drive')
Once you execute this code you will see a message:
You will be asked to login to your Gmail account and authorize the access permissions to Colab.
Step 2: Copy path of your file
Next you can go to the file icon on the left sidebar in Google Drive and expand drive -> MyDrive and copy path of your file (csv, txt, xlsx or any other)
drive_csv_file_path = '/content/drive/MyDrive/data/data.csv'
Step 3: All good to use it in your Python Code!
Example:import csv
def read_from_csv_file(data_file):
with open(data_file, 'r') as file:
csv_reader = csv.reader(file)
for row in csv_reader:
print(row)
drive_csv_file_path = '/content/drive/MyDrive/data/data.csv'
read_from_csv_file(drive_csv_file_path)
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
- Java code to check Internet Connection on Android Device Programmatically - Android
- How to Open Trash on Mac - MacOS
- How to create a SharePoint list from an existing list with same columns, views, formatting, data - SharePoint
- Install RabbitMQ on Docker - Docker
- Cannot access Windows application shortcuts on Start menu and Taskbar - Windows
- Install Homebrew (brew) on Mac M2 - MacOS
- Clear browsing history in Safari on Mac Ventura 13 - MacOS
- WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip. - PIP