├── Download-Large-File-from-Google-Drive.ipynb └── README.md /Download-Large-File-from-Google-Drive.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "#taken from this StackOverflow answer: https://stackoverflow.com/a/39225039\n", 12 | "import requests\n", 13 | "\n", 14 | "def download_file_from_google_drive(id, destination):\n", 15 | " URL = \"https://docs.google.com/uc?export=download\"\n", 16 | "\n", 17 | " session = requests.Session()\n", 18 | "\n", 19 | " response = session.get(URL, params = { 'id' : id }, stream = True)\n", 20 | " token = get_confirm_token(response)\n", 21 | "\n", 22 | " if token:\n", 23 | " params = { 'id' : id, 'confirm' : token }\n", 24 | " response = session.get(URL, params = params, stream = True)\n", 25 | "\n", 26 | " save_response_content(response, destination) \n", 27 | "\n", 28 | "def get_confirm_token(response):\n", 29 | " for key, value in response.cookies.items():\n", 30 | " if key.startswith('download_warning'):\n", 31 | " return value\n", 32 | "\n", 33 | " return None\n", 34 | "\n", 35 | "def save_response_content(response, destination):\n", 36 | " CHUNK_SIZE = 32768\n", 37 | "\n", 38 | " with open(destination, \"wb\") as f:\n", 39 | " for chunk in response.iter_content(CHUNK_SIZE):\n", 40 | " if chunk: # filter out keep-alive new chunks\n", 41 | " f.write(chunk)" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 2, 47 | "metadata": { 48 | "collapsed": true 49 | }, 50 | "outputs": [], 51 | "source": [ 52 | "file_id = '0B1fGSuBXAh1IeEpzajRISkNHckU'\n", 53 | "destination = '/home/myusername/work/myfile.ext'\n", 54 | "download_file_from_google_drive(file_id, destination)" 55 | ] 56 | } 57 | ], 58 | "metadata": { 59 | "kernelspec": { 60 | "display_name": "Python 3", 61 | "language": "python", 62 | "name": "python3" 63 | }, 64 | "language_info": { 65 | "codemirror_mode": { 66 | "name": "ipython", 67 | "version": 3 68 | }, 69 | "file_extension": ".py", 70 | "mimetype": "text/x-python", 71 | "name": "python", 72 | "nbconvert_exporter": "python", 73 | "pygments_lexer": "ipython3", 74 | "version": "3.5.2" 75 | } 76 | }, 77 | "nbformat": 4, 78 | "nbformat_minor": 2 79 | } 80 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Download-Large-File-From-Google-Drive-Using-Python 2 | 3 | This is a simple yet effective method to download LARGE files from google drive using Python 4 | I have only tried it with Python 3.6 5 | I have NOT tried it with Folders instead of Files 6 | 7 | I have only taken the python code in [this stackoverflow answer](https://stackoverflow.com/a/39225039) and put it in a IPython Notebook 8 | 9 | All you need to do is this: 10 | 1) Get the file ID of your file on Google Drive (i.e. from the sharable link) 11 | 2) Paste the file ID in file_id 12 | 3) Specify the full path of where you want to save the downloaded file 13 | 4) call the function download_file_from_google_drive(file_id, destination) 14 | 15 | ## Example: 16 | ``` 17 | file_id = '0B6lEo20DNMISIJDSJDVBMENXbkE' 18 | destination = '/home/myusername/work/myfile.ext' 19 | download_file_from_google_drive(file_id, destination) 20 | ``` 21 | --------------------------------------------------------------------------------