So far I can upload file to the folder if it exists. I can't figure out a way to create one though. So if the folder does not exist, my script dies.
import sys
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gpath = '2015'
fname = 'Open Drive Replacements 06_01_2015.xls'
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file1 in file_list:
if file1['title'] == gpath:
id = file1['id']
file1 = drive.CreateFile({'title': fname, "parents": [{"kind": "drive#fileLink","id": id}]})
file1.SetContentFile(fname)
file1.Upload()
Can you please help me modify the above code to create folder gpath if it does not exist?
Based on the documentation, it should be
file1 = drive.CreateFile({'title': fname,
"parents": [{"id": id}],
"mimeType": "application/vnd.google-apps.folder"})
Update: As of Apr 2020, documentation (v3) has been updated with API docs and shows:
folder_id = '0BwwA4oUTeiV1TGRPeTVjaWRDY1E'
file_metadata = {
'name': 'photo.jpg',
'parents': [folder_id]
}
media = MediaFileUpload('files/photo.jpg',
mimetype='image/jpeg',
resumable=True)
file = drive_service.files().create(body=file_metadata,
media_body=media,
fields='id').execute()
print 'File ID: %s' % file.get('id')