【问题标题】:google drive api to upload all pdfs to google drivegoogle drive api将所有pdf上传到google drive
【发布时间】:2019-09-05 05:36:13
【问题描述】:

我正在使用 pydrive 将 pdf 文件上传到我的 google drive 文件夹。我想使用此代码一次将所有*pdf 文件发送到本地文件夹中,但不知道从哪里开始?我应该使用glob 吗?如果是这样,我想看一个例子,请。

将 1 个文件发送到指定的 google 驱动器文件夹的工作代码:

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(g_login)

folder_id = 'google_drive_id_goes_here'
f = drive.CreateFile({'title': 'testing_pdf',
                      'mimeType': 'application/pdf',
                      'parents': [{'kind': 'drive#fileLink', 'id':folder_id}]})
f.SetContentFile('/Users/Documents/python/google_drive/testing.pdf')
f.Upload()

【问题讨论】:

    标签: python python-3.x google-drive-api google-api-dotnet-client pydrive


    【解决方案1】:

    您不能一次上传文件。使用 API 创建文件是一回事,而 pydrive 作为 uploading 的机制不止一个。

    你必须把它放在一个循环中,并在你去的时候上传每个文件。

    import os
    directory = 'the/directory/you/want/to/use'
    
    for filename in os.listdir(directory):
        if filename.endswith(".txt"):
            f = open(filename)
            lines = f.read()
            print (lines[10])
            continue
        else:
        continue
    

    【讨论】:

    • 得到了 DalmTo - 将使用循环尝试一些东西。感谢提示。
    猜你喜欢
    • 2020-08-11
    • 2018-11-11
    • 2020-03-10
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    相关资源
    最近更新 更多