Google pydrive uploading a file to specific folder

…衆ロ難τιáo~ 提交于 2020-02-02 16:03:10

问题


I am trying to upload a file to my Google drive, the code below works. How can I specify to which folder to upload to i.e drive---shared with me--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()

回答1:


  • You want to upload a file to the specific folder in your Google Drive using pydrive.

If my understanding is correct, how about this modification?

From:

file2 = drive.CreateFile()

To:

file2 = drive.CreateFile({'parents': [{'id': '### folder ID ###'}]})
  • Please set the folder ID like above.

Reference:

  • PyDrive’s documentation

If this was not the result you want, I apologize.

Added:

When you want to upload a file to the specific folder from the folder name, how about this modification?

From:

file2 = drive.CreateFile()
file2.SetContentFile('new_test.csv')
file2.Upload()

To:

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


来源:https://stackoverflow.com/questions/56434084/google-pydrive-uploading-a-file-to-specific-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!