16

I am trying to load csv file stored in google drive to colab notebooks. When i try to load the file it is showing "File not found". What is the procedure to load files stored in google drive to colab notebooks??

2

4 Answers 4

11

easiest way I found is mounting google drive in colab:

from google.colab import drive drive.mount('/content/gdrive') 

then use '/content/gdrive/My Drive/' as prefix of the file path. suppose you have a text file in data directory of you google drive. Then you can access it with following code:

open('/content/gdrive/My Drive/data/filename.txt').read() 
Sign up to request clarification or add additional context in comments.

2 Comments

This should be marked as the solution. The authentication process is very simple
Thanks! This worked for me. I have given an incorrect file path & I was getting file not found error. So copied the file path by right clicking on the file & read it as dataset = pd.read_csv('/content/gdrive/MyDrive/Data Mining ML Cw1/Clustering/1_Resources/Happiness-Data (2).csv') & it worked.
8

For accessing file from Google Drive, you need to load file using PyDrive or Drive Rest API.

Run below code before accessing file

!pip install -U -q PyDrive from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive from google.colab import auth from oauth2client.client import GoogleCredentials # 1. Authenticate and create the PyDrive client. auth.authenticate_user() gauth = GoogleAuth() gauth.credentials = GoogleCredentials.get_application_default() drive = GoogleDrive(gauth) # 2. Load a file by ID and create local file. downloaded = drive.CreateFile({'id':'fileid'}) # replace fileid with Id of file you want to access downloaded.GetContentFile('export.csv') # now you can use export.csv 

2 Comments

What is id? Can't be sure on that.
if you try to get shareable link for some file in google drive, there will be a id parameter.
4

Try:

from google.colab import drive drive.mount('/content/drive') 

This commands will bring you to a Google Authentication step. You should see a screen with Google Drive File Stream wants to access your Google Account. After you allow permission, copy the given verification code and paste it in the box in Colab.

In the notebook, click on the charcoal > on the top left of the notebook and click on Files. Locate the data folder you created earlier and find your data. Right-click on your data and select Copy Path. Store this copied path into a variable and you are ready to go.

file = "copied path" df = pd.read_csv(file) df.head() 

TIP: Add a slash (/) as part of the name of directory (for Linux or Mac users). Eg: "/content/drive/My Drive/Colab Notebooks/data/xpto.csv"

Comments

1
from google.colab import files files.upload() 

add those lines then you can manually upload the file, I hope it helps.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.