Skip to content

Instantly share code, notes, and snippets.

@RodrigoCMoraes
Created May 27, 2019 00:37
Show Gist options
  • Save RodrigoCMoraes/e179a67f1c36be57c82ca54cc1bfcd4b to your computer and use it in GitHub Desktop.
Save RodrigoCMoraes/e179a67f1c36be57c82ca54cc1bfcd4b to your computer and use it in GitHub Desktop.
Getting file from Google Drive by PyDrive
#@title Dataset info { form-width: "800px" }
# e.g GOOGLE_DRIVE_FILE_URL: https://drive.google.com/open?id=1CZyDkHHjS8SUVBF7fBwoTAF6xSpLmD4P7
LinkFile = "GOOGLE_DRIVE_FILE_URL" #@param {type:"string"}
# e.g GOOGLE_DRIVE_FILENAME_WITH_EXTENSION: Local History.csv
Filename = "GOOGLE_DRIVE_FILENAME_WITH_EXTENSION" #@param {type:"string"}
Filename = Filename.replace(' ', '\ ')
# Install the PyDrive wrapper & import libraries.
# This only needs to be done once per notebook.
!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
# Authenticate and create the PyDrive client.
# This only needs to be done once per notebook.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
# Download a file based on its file ID.
#
# A file ID looks like: laggVyWshwcyP6kEI-y_W3P8D26sz
file_id = LinkFile.split('=')[-1]
downloaded = drive.CreateFile({'id': file_id})
downloaded.GetContentFile(Filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment