python - Google pydrive 将文件上传到特定文件夹
问题描述
我正在尝试将文件上传到我的 Google 驱动器,下面的代码有效。如何指定要上传到即驱动器的文件夹---与我共享--csvFolder
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
file2 = drive.CreateFile()
file2.SetContentFile('new_test.csv')
file2.Upload()
解决方案
- 您想使用 pydrive 将文件上传到 Google Drive 中的特定文件夹。
如果我的理解是正确的,那么这个修改呢?
从:
file2 = drive.CreateFile()
至:
file2 = drive.CreateFile({'parents': [{'id': '### folder ID ###'}]})
- 请像上面一样设置文件夹ID。
参考:
如果这不是你想要的结果,我很抱歉。
添加:
当你想从文件夹名称上传文件到特定文件夹时,这个修改怎么样?
从:
file2 = drive.CreateFile()
file2.SetContentFile('new_test.csv')
file2.Upload()
至:
folderName = '###' # Please set the folder name.
folders = drive.ListFile(
{'q': "title='" + folderName + "' and mimeType='application/vnd.google-apps.folder' and trashed=false"}).GetList()
for folder in folders:
if folder['title'] == folderName:
file2 = drive.CreateFile({'parents': [{'id': folder['id']}]})
file2.SetContentFile('new_test.csv')
file2.Upload()
获取文件夹 ID 的替代方法
您可以使用以下代码段打印文件和/或文件夹 ID
fileList = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file in fileList:
print('Title: %s, ID: %s' % (file['title'], file['id']))
# Get the folder ID that you want
if(file['title'] == "To Share"):
fileID = file['id']
推荐阅读
- kubernetes - Kubernetes - 垂直扩展在不终止任何实例的情况下增加 CPU 使用率
- embedded-linux - 从闪存 (XIP) 运行内核和用户空间应用程序?
- html - HTML 可排序,一行中有更多单元格?
- python - 多处理函数不会更新python中的数据框
- python - 安装mysql连接器时出现问题错误
- python - 我可以修改列表中指定的单个值吗?
- android - 在打开 Android Studio 时,我在正文中收到错误链接?
- c# - c#反射,如何获取泛型类型类属性
- javascript - 使用 getter 和方法返回对象的函数
- java - Jenkins 上的 Sonarscanner:在 JAVA_HOME 或 PATH 中找不到“java”可执行文件