avinash
avinash

Reputation: 175

Csv file not found error in Google colaboratory notebooks

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??

Upvotes: 16

Views: 34229

Answers (4)

Andre Araujo
Andre Araujo

Reputation: 2400

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"

Upvotes: 4

elyte5star
elyte5star

Reputation: 321

from google.colab import files

files.upload()

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

Upvotes: 1

priojeet priyom
priojeet priyom

Reputation: 908

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()

Upvotes: 11

Dharmendra
Dharmendra

Reputation: 308

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 

Upvotes: 8

Related Questions