Reputation: 3086
I'm new to Colaboratory and would like setup a small project which is stored on my google drive. On my google drive, I create a folder 'TheProject', where I created two folders: 'code' and 'data'. I the folder 'code' I created a new colab notebook and I have several data sets in 'data' folder.
QUESTION
How to read data into colab notebook from a folder on the google drive? For example:
data = pd.read_excel('SOME_PATH/TheProject/data/my_data.xlsx')
where SOME_PATH
should indicate how to get to the main folder 'TheProject' and read data from 'data' folder.
Upvotes: 3
Views: 5674
Reputation: 851
Right click on your file on Google Drive and get its sharable link. from that link you'll extract the file id.
! pip install pydrive
# these classes allow you to request the Google drive API
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)
file_id = '<your_file_id>'
downloaded = drive.CreateFile({'id': file_id})
# allows you to temporarily load your file in the notebook VM
# assume the file is called file.csv and it's located at the root of your drive
downloaded.GetContentFile('file.csv')
Once you hit these commands you will be prompted a link that asks you to grant permission to Google Drive. It'll give you a token that you must type in a text box.
Now you're ready to load your file:
data = pd.read_csv('file.csv')
Upvotes: 8