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:
![Permit this notebook to access your Google Drive files Permit this notebook to access your Google Drive files](https://code2care.org/howto/google-colab-read-rile-from-google-drive/images/Permit this notebook to access your Google Drive files.jpg)
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'
![Colab copy file path from drive folder Colab copy file path from drive folder](https://code2care.org/howto/google-colab-read-rile-from-google-drive/images/Colab copy file path from drive folder.jpg)
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
- How to Search Maven Central Repository - HowTos
- How to Select Two Separate Columns in Excel on Mac - Microsoft
- How to create a tar.gz file using Java - Java
- How to Concatenate Strings in Bash Scripting - Bash
- 'Edit Document' Requires a Windows Sharepoint Services-compatible application and Microsoft Internet Explorer 6.0 or higher - SharePoint
- Bash: Command to Find the Length of a String - Bash
- Twitter is down? Issues with Tweet create events affecting APIs - Twitter
- Python: How to Save Image from URL - Python