使用 python 的谷歌驱动器文件夹中的文件列表

新手上路,请多包涵
阅读 643
2 个回答

您可以在此处查看有关如何在云端硬盘中列出文件的示例: https ://developers.google.com/drive/api/v3/search-files。您需要构建一个列出文件夹中文件的查询:使用

q = "'1234' in parents"

其中 1234 是您要列出的文件夹的 ID。您可以修改查询以列出特定类型的所有文件(例如文件夹中的所有 jpeg 文件)等。

原文由 Luca 发布,翻译遵循 CC BY-SA 4.0 许可协议

这是一个 hacky-yet-successful 解决方案。这实际上是从一个特定的 Google Drive 文件夹(在本例中是一个名为“thumbnails”的文件夹) 获取 所有文件。我需要从特定文件夹中获取(不仅仅是列出)所有文件并对它们执行图像调整,因此我使用了以下代码:

 `# First, get the folder ID by querying by mimeType and name
folderId = drive.files().list(q = "mimeType = 'application/vnd.google-apps.folder' and name = 'thumbnails'", pageSize=10, fields="nextPageToken, files(id, name)").execute()
# this gives us a list of all folders with that name
folderIdResult = folderId.get('files', [])
# however, we know there is only 1 folder with that name, so we just get the id of the 1st item in the list
id = folderIdResult[0].get('id')

# Now, using the folder ID gotten above, we get all the files from
# that particular folder
results = drive.files().list(q = "'" + id + "' in parents", pageSize=10, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])

# Now we can loop through each file in that folder, and do whatever (in this case, download them and open them as images in OpenCV)
for f in range(0, len(items)):
    fId = items[f].get('id')
    fileRequest = drive.files().get_media(fileId=fId)
            fh = io.BytesIO()
            downloader = MediaIoBaseDownload(fh, fileRequest)
            done = False
            while done is False:
                status, done = downloader.next_chunk()
    fh.seek(0)
    fhContents = fh.read()

    baseImage = cv2.imdecode(np.fromstring(fhContents, dtype=np.uint8), cv2.IMREAD_COLOR)

原文由 todbott 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题