问题
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?
回答1:
Based on the documentation, it should be
file1 = drive.CreateFile({'title': fname,
"parents": [{"id": id}],
"mimeType": "application/vnd.google-apps.folder"})
来源:https://stackoverflow.com/questions/30585166/create-a-folder-if-not-exists-on-google-drive-and-upload-a-file-to-it-using-py