Loading files to Google Drive using python

This guide will walkthrough the set up for being able to load csv and excel files to Google Drive with python.

Uploading files to Google Drive is a useful to have as you can upload much larger data sets than can be loaded into Google Sheets (Check out this tutorial for how to upload data to Google Sheets with python).

As usual with python there is a package that takes cares of most of the legwork. We will be using the PyDrive package to upload files to Google Drive.

Prerequisites:

  1. Python with PyDrive installed
  2. Google Account

The PyDrive package has documentation to get set up using the tool. This is what I followed below, only with more screenshots.

 

Setting up Authorisation

Go to the Google API console and select Drive API

 

 

If this is the first time using the Google API console we may need to accept some terms and conditions

 

To enable the Google Drive API we need to first create a project.

Click “Create”

Give the project a name and click “Create”

This will make the project and take us back to the main Google Drive API screen where we can now enable this functionality.

Now we need to navigate to the credentials area to download some credentials we can use.

Select “Other UI (e.g. Windows, CLI tool)” in the first drop down and “User data” in the radio buttons options. Then click “What credentials do I need”?

We should now be at a screen where we are setting up client details for an “OAuth 2.0 client ID“.

Name the client and click “Create client ID”

Give the product a name and click “Continue”

 

Click “Download” to download the credentials then hit “Done”

We now have a client_id.json file on our computer. This needs to be renamed to “client_secrets.json and placed in your working directory.

Run Some Code

Run the following code in a python interpreter

from pydrive.auth import GoogleAuth

gauth = GoogleAuth()
gauth.LocalWebserverAuth()

drive = GoogleDrive(gauth)

file1 = drive.CreateFile({'title': 'Hello.txt'})  # Create GoogleDriveFile instance with title 'Hello.txt'.
file1.SetContentString('Hello World!') # Set content of the file from given string.
file1.Upload()

This will open a new tab in your browser asking you to allow the project we just made in the Google API console. Click “allow”.

 

Then this code snippet will upload a text file into your Google drive called Hello.txt, with “Hello world” inside. What we really want however is to upload large csv files that are stored on local drives.

This snippet (following allowing the project access) will upload a local csv file called “something.csv” into a Google Drive account.

file1 = drive.CreateFile({"mimeType": "text/csv"})
file1.SetContentFile("something.csv")
file1.Upload()

 

Congratulations, you made it to the end! If you followed along you now have the ability to upload text and csv files to Google drive. Another tool in the tool belt to tame any data that might come your way.

8 thoughts on “Loading files to Google Drive using python”

    1. Hi QF, you are right. I missed the line defining what drive is here: ‘drive = GoogleDrive(gauth)’. Ive updated the code snippet now, this should work for you.

    1. Hi, creating a project in the Google Cloud Console doesn’t cost anything. However there are many different tools to make use of within a project. Enabling the Google Drive API doesn’t have any associated costs. If you were to use other tools such as the Cloud Vision API or Big Query then those will have costs.

Send a Comment

Your email address will not be published.