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
- Download a SSL Certificate from a URL in Terminal - Bash
- Command: How to scp a file to remote server location? - HowTos
- SharePoint installation - Appfabric installation failed because installer MSI returned with error code:1603 - SharePoint
- Trigger Flow on selected Listitem from SharePoint view - create button with JSON column formatting - SharePoint
- Restore deleted Office 365 SharePoint group site - SharePoint
- Convert Java String to JSON Object using Jackson - Java
- How to Run Terminal As Admin on Mac - MacOS
- Remove git config at Local, Global or System Levels? - Git