Last active
November 24, 2018 22:29
-
-
Save dipunj/1f9df9c84c405b1ff9f79ce59090ac7c to your computer and use it in GitHub Desktop.
Download and upload files to/from google drive from /to google colab using pydrive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "googleColab_drive.ipynb", | |
"version": "0.3.2", | |
"views": {}, | |
"default_view": {}, | |
"provenance": [], | |
"collapsed_sections": [] | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"metadata": { | |
"id": "T9lmxL942nH9", | |
"colab_type": "code", | |
"colab": { | |
"autoexec": { | |
"startup": false, | |
"wait_interval": 0 | |
} | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"!pip install -U -q PyDrive\n", | |
"from pydrive.auth import GoogleAuth\n", | |
"from pydrive.drive import GoogleDrive\n", | |
"from google.colab import auth\n", | |
"from oauth2client.client import GoogleCredentials\n", | |
"\n", | |
"# Authenticate and create the PyDrive client.\n", | |
"auth.authenticate_user()\n", | |
"gauth = GoogleAuth()\n", | |
"gauth.credentials = GoogleCredentials.get_application_default()\n", | |
"drive = GoogleDrive(gauth)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "W-cHp2ql2uIT", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"# To upload a file to google drive :" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "LKSmlTlk2r0t", | |
"colab_type": "code", | |
"colab": { | |
"autoexec": { | |
"startup": false, | |
"wait_interval": 0 | |
} | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"# Save the file.csv to drive\n", | |
"\n", | |
"# Create a file named TargetFileNameInDrive.csv in your drive account\n", | |
"uploaded = drive.CreateFile({'title': 'TargetFileNameInDrive.csv'})\n", | |
"\n", | |
"# Name of the file in colab notebook's \"current files\"\n", | |
"uploaded.SetContentFile('file.csv')\n", | |
"\n", | |
"# Initiate the upload\n", | |
"uploaded.Upload()\n", | |
"\n", | |
"# Will show the file ID on successful upload to drive.\n", | |
"print('Uploaded file with ID {}'.format(uploaded.get('id')))" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "HaYaPoiX4KX_", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"# To download a file from google drive :" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "xsm0evMI4Oml", | |
"colab_type": "code", | |
"colab": { | |
"autoexec": { | |
"startup": false, | |
"wait_interval": 0 | |
} | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"downloaded = drive.CreateFile({'id': '<id_of_your_file_in_google_drive>'})\n", | |
"downloaded.GetContentFile('file.csv') # Download file to colab as 'file.csv'" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! Much needed.