Skip to content

Instantly share code, notes, and snippets.

@loctv
Forked from swma24/file_upload_google_drive.py
Created November 8, 2023 03:06
Show Gist options
  • Select an option

  • Save loctv/afb423577304f62f66ed98c657964604 to your computer and use it in GitHub Desktop.

Select an option

Save loctv/afb423577304f62f66ed98c657964604 to your computer and use it in GitHub Desktop.
file upload test using pydrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import time
gauth = GoogleAuth()
gauth.LoadCredentialsFile("mycreds.txt")
if gauth.credentials is None:
# Authenticate if they're not there
gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
# Refresh them if expired
gauth.Refresh()
else:
# Initialize the saved creds
gauth.Authorize()
# Save the current credentials to a file
gauth.SaveCredentialsFile("mycreds.txt")
drive = GoogleDrive(gauth)
date = int(time.strftime("%Y%m%d"))
upload_file_name = "{0}_{1}.csv".format('test', date)
f = drive.CreateFile({'title': upload_file_name, 'parents': [{'id': '...'}]})
f.SetContentFile(filename)
f.Upload()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment