Google pydrive将文件上传到特定文件夹 [英] Google pydrive uploading a file to specific folder

查看:194
本文介绍了Google pydrive将文件上传到特定文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件上传到我的Google驱动器,下面的代码有效. 如何指定要上传到即驱动器的文件夹-与我共享的文件夹-csvFolder

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

推荐答案

  • 您要使用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']
      

      这篇关于Google pydrive将文件上传到特定文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆