在google驱动器上创建一个文件夹(如果不存在),并使用Python scrip将文件上载到其中

2024-06-16 11:08:51 发布

您现在位置:Python中文网/ 问答频道 /正文

到目前为止,我可以上传文件到文件夹,如果它存在。我想不出一个办法来创造一个。所以如果文件夹不存在,我的脚本就死了。

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()

如果上面的代码不存在,您能帮助我修改它来创建文件夹gpath吗?


Tags: infromimport文件夹iddrivefnamefile1
1条回答
网友
1楼 · 发布于 2024-06-16 11:08:51

基于documentation,应该是

file1 = drive.CreateFile({'title': fname, 
    "parents":  [{"id": id}], 
    "mimeType": "application/vnd.google-apps.folder"})

相关问题 更多 >