Can\'t read a file in google colaboratory . I have .ipynb file and .csv files in the same directory but when I try to run:
train = pd.read_csv(\"train.csv\")
<
I hope you have run this code before opening your train
file.
# Install a Drive FUSE wrapper.
# https://github.com/astrada/google-drive-ocamlfuse
!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse
# Generate auth tokens for Colab
from google.colab import auth
auth.authenticate_user()
# Generate creds for the Drive FUSE library.
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}
# Create a directory and mount Google Drive using that directory.
!mkdir -p drive
!google-drive-ocamlfuse drive
print ('Files in Drive:')
!ls drive/
After implementing the above codes just open your train
file in google-colaboratory
train = pd.read_csv('drive/...{folder_name}.../train.csv, encoding='utf8')
I hope this helps!
I use windows 10 and this worked perfectly for me. Give it try.
Add new folder into your drive. Name it what you want. In my case I named it "Colab Notebook". This is folder where I keep my codes and data file.
First you need to mount your drive. For this run the following one by one
from google.colab import drive
drive.mount('/content/drive/')
After second command it pops link where the authentication key is. Open this link copy the key, paste and press enter.
Now type !ls
it has to give something like this drive sample_data
Upload your data file. Either it will be csv or excel file does not matter, but commands will be different for each.
For csv file
train = pd.read_csv('/content/drive/My Drive/Colab Notebook/train.csv')
For excel file it's the same just change pandas command and file extension
!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
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
file_id = 'REPLACE_WITH_YOUR_FILE_ID'
downloaded = drive.CreateFile({'id': file_id})
downloaded = drive.CreateFile({'id':'1BH-rffqv_1auzO7tdubfaOwXzf278vJK'}) # replace the id with id of file you want to access
downloaded.GetContentFile('xyz.csv')
# Read file as panda dataframe
import pandas as pd
xyz = pd.read_csv('xyz.csv')
make sure you put a / in the beginning of the path. Whey you copy path by right click on your mounted drive from the file explorer, it does not copy the beginning forward slash. make sure you add it in the path.