Connect To Google Drive File

Photo by Firmbee.com on Unsplash

Connect To Google Drive File

prep dependencies:

# libraries for the files in google drive
from pydrive.auth import GoogleAuth
from google.colab import drive
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

prep googledrive connection:

import pandas as pd
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
#https://drive.google.com/file/d/1lbDi1pPmvrWiL8MQLbaLpMl15HjNbz1j/view?usp=share_link
file_id = '1lbDi1pPmvrWiL8MQLbaLpMl15HjNbz1j' #<-- You add in here the id from you google drive file, you can find it


f = drive.CreateFile({'id': file_id })
f.FetchMetadata(fetch_all=True)
print(f['title'])

output:

read using pandas:

# Download the file to a local disc
f.GetContentFile(f['title'])
df  = pd.read_csv(f['title'])
df.head()

output:

colab notebook:

https://colab.research.google.com/drive/1Kt8YSX_WxaMoE5Aw7vnwstXry-iaVK5R?usp=sharing