Sign inGet started
← Back to all guides

How to download files from Google Drive in Python

By Nick Barth

Updated on March 6, 2024

To download files from Google Drive using Python, you'll typically want to use the `google-api-python-client` and `google-auth-httplib2` libraries to interact with the Google Drive API. Below is a basic outline of steps you would follow:

  1. Set up a Google Cloud project and enable the Drive API.
  2. Obtain the necessary credentials and save them in a file, usually `credentials.json`.
  3. Install the required Python libraries by running the command `pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib`.
  4. Use the following Python code as a starting point to authenticate and download the desired file:
from googleapiclient.discovery import build
⁠
⁠from googleapiclient.http import MediaIoBaseDownload
⁠
⁠from google_auth_oauthlib.flow import InstalledAppFlow
⁠
⁠from io import BytesIO

Define the scopes

SCOPES = ['https://www.googleapis.com/auth/drive']

Function to authenticate and create the service

def create_service():
⁠
⁠    flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
⁠
⁠    creds = flow.run_local_server(port=0)
⁠
⁠    return build('drive', 'v3', credentials=creds)

Function to download file

def download_file(service, file_id, file_name):
⁠
⁠    request = service.files().get_media(fileId=file_id)
⁠
⁠    fh = BytesIO()
⁠
⁠    downloader = MediaIoBaseDownload(fd=fh, request=request)
⁠
⁠    done = False
⁠
⁠    while not done:
⁠
⁠        status, done = downloader.next_chunk()
⁠
⁠        print("Download Progress: {0}".format(int(status.progress() * 100))) 
    fh.seek(0)
⁠
⁠    with open(file_name, 'wb') as f:
⁠
⁠        f.write(fh.read())
⁠
⁠        f.close()

Main function to initiate download process

if name == 'main':
⁠
⁠    service = create_service()
⁠
⁠    file_id = 'FILE_ID'  # Replace with the actual file ID
⁠
⁠    file_name = 'FILE_NAME'  # Replace with the desired file name
⁠
⁠    download_file(service, file_id, file_name)

Ensure you replace `'FILE_ID'` and `'FILE_NAME'` with the actual ID of the Google Drive file you wish to download and the desired name for your downloaded file.

To download files from Google Drive to Deepnote, you'll first need to ensure that you have the correct permissions to access the files on Google Drive. Once you have access, you can use the following steps to download the files into your Deepnote project:

  1. In Deepnote, click on the Integrations tab on the left sidebar.
  2. Find and select Google Drive from the list of available integrations.
  3. Connect your Google Drive account by following the authentication process.
  4. Navigate to your project's files section.
  5. Click on 'Add file', then select 'Import from Google Drive'.
  6. Browse and select the files you wish to download from your Google Drive.
  7. Once selected, confirm to import the files into your Deepnote project's workspace.

The selected files will now be downloaded and available in your Deepnote project directory, ready for use in your data analysis or other computational tasks. Remember to respect any data privacy or sharing agreements associated with the files you are accessing.

Nick Barth

Product Engineer

Nick has been interested in data science ever since he recorded all his poops in spreadsheet, and found that on average, he pooped 1.41 times per day. When he isn't coding, or writing content, he spends his time enjoying various leisurely pursuits.

Follow Nick on LinkedIn and GitHub

That’s it, time to try Deepnote

Get started – it’s free
Book a demo

Footer

Solutions

  • Notebook
  • Data apps
  • Machine learning
  • Data teams

Product

Company

Comparisons

Resources

  • Privacy
  • Terms

© Deepnote