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)
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!