python - 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()
解决方案
您不能一次上传文件。使用 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
推荐阅读
- python - 使用 pandas.read_csv 从 python 中具有分隔符的 csv 读取内容
- java - 当我第二次迭代数组时如何不覆盖数组
- python - BeautifulSoup:HTML 提取项目符号而不是导航栏
- angular - 使用 Angular 日期格式,但不在管道中
- git - Azure DevOps 使用 Git 子模块的问题
- python - 为什么 random.random() 在 Python 中不安全?
- apache-kafka - Json文件数据导入kafka主题
- sql-server - 如何在不添加额外数字的情况下将实数转换为浮点数
- android - 引发意图,但仅在调试模式下执行操作
- scikit-learn - 如何可视化来自 scikitlearn 的最近邻算法