Python:从 google 驱动器中查找并下载丢失的文件(使用可共享的 link)
Python: find and download missing files from google drive (using a sharable link)
给定一个可共享的 link 到 google 驱动器文件夹(文件夹 ID),我想将此文件夹下的目录列表与给定路径下的目录列表进行比较,并下载缺少文件。
我读过有关 PyDrive
的内容,但找不到无需身份验证即可访问驱动器文件夹的优雅方法。
例如:
files_under_gdrive = ["File1", "File2", "File3"]
files_under_given_path = ["File1", "some_other_file"]
# Download missing files found only in Google Drive
...
files_under_given_path = ["File1", "some_other_file", "File2", "File3"]
任何 hint/idea 将不胜感激。谢谢:)
例如,您可以轻松地从本地目录收集文件并将其名称存储在列表中。
之后,为了从 Google 驱动器共享驱动器中检索文件,您可以使用 Files.list
请求:
GET https://www.googleapis.com/drive/v3/files
将includeItemsFromAllDrives
设置为true
,driveId
字段设置为共享驱动器的相应id。根据您的具体需要和要求,您也可以向请求中添加其他字段。
从共享 Google 驱动器中检索文件后,您可以简单地比较两个列表,并根据结果下载所需的文件。要下载文件,您可能需要从 Drive API documentation:
查看此片段
file_id = 'ID_OF_THE_FILE_TO_DOWNLOAD'
request = drive_service.files().get_media(fileId=file_id)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print "Download %d%%." % int(status.progress() * 100)
但是,我建议您先完成 Google Drive Quick Start with Python from here.
参考
给定一个可共享的 link 到 google 驱动器文件夹(文件夹 ID),我想将此文件夹下的目录列表与给定路径下的目录列表进行比较,并下载缺少文件。
我读过有关 PyDrive
的内容,但找不到无需身份验证即可访问驱动器文件夹的优雅方法。
例如:
files_under_gdrive = ["File1", "File2", "File3"]
files_under_given_path = ["File1", "some_other_file"]
# Download missing files found only in Google Drive
...
files_under_given_path = ["File1", "some_other_file", "File2", "File3"]
任何 hint/idea 将不胜感激。谢谢:)
例如,您可以轻松地从本地目录收集文件并将其名称存储在列表中。
之后,为了从 Google 驱动器共享驱动器中检索文件,您可以使用 Files.list
请求:
GET https://www.googleapis.com/drive/v3/files
将includeItemsFromAllDrives
设置为true
,driveId
字段设置为共享驱动器的相应id。根据您的具体需要和要求,您也可以向请求中添加其他字段。
从共享 Google 驱动器中检索文件后,您可以简单地比较两个列表,并根据结果下载所需的文件。要下载文件,您可能需要从 Drive API documentation:
查看此片段file_id = 'ID_OF_THE_FILE_TO_DOWNLOAD'
request = drive_service.files().get_media(fileId=file_id)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print "Download %d%%." % int(status.progress() * 100)
但是,我建议您先完成 Google Drive Quick Start with Python from here.