首页 > 解决方案 > google drive api将所有pdf上传到google drive

问题描述

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

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

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

标签: pythonpython-3.xgoogle-drive-apigoogle-api-dotnet-clientpydrive

解决方案


您不能一次上传文件。使用 API 创建文件是一回事,而 pydrive 没有用于上传多个 .

您必须将其置于循环中并随时上传每个文件。

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

推荐阅读