且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Google pydrive将文件上传到特定文件夹

更新时间:2022-11-05 12:11:25

  • 您要使用pydrive将文件上传到Google云端硬盘中的特定文件夹.
  • 如果我的理解是正确的,那么该修改如何?

    If my understanding is correct, how about this modification?

file2 = drive.CreateFile()

收件人:

file2 = drive.CreateFile({'parents': [{'id': '### folder ID ###'}]})

  • 请像上面一样设置文件夹ID.
  • 如果这不是您想要的结果,我表示歉意.

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

    要从文件夹名称将文件上传到特定文件夹时,如何进行此修改?

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

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

Alternative to get folder ID

You can use the following snippet to print files and or folders 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']