谷歌驱动器的API上传所有PDF到谷歌驱动器

时间:2019-09-05 05:36:13

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

我正在使用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()

1 个答案:

答案 0 :(得分:0)

您无法一次上传文件。使用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