pro*_*z32 12 python google-api-client google-drive-api 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()
Run Code Online (Sandbox Code Playgroud)
Tan*_*ike 16
如果我的理解是正确的,这个修改怎么样?
file2 = drive.CreateFile()
Run Code Online (Sandbox Code Playgroud)
file2 = drive.CreateFile({'parents': [{'id': '### folder ID ###'}]})
Run Code Online (Sandbox Code Playgroud)
如果这不是您想要的结果,我深表歉意。
当您想从文件夹名称上传文件到特定文件夹时,如何修改?
file2 = drive.CreateFile()
file2.SetContentFile('new_test.csv')
file2.Upload()
Run Code Online (Sandbox Code Playgroud)
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()
Run Code Online (Sandbox Code Playgroud)
您可以使用以下代码段打印文件和/或文件夹 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']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7104 次 |
| 最近记录: |