python - 如何在Google Drive中搜索特定文件?

标签 python python-3.x google-api google-drive-api google-api-python-client

我从 Google 开发者网站获取了这段代码,并对其进行了一些更改。但我想要搜索 Google 云端硬盘中存在的文件。我不知道在哪里提供文件夹 ID 来查找文件?如果它们存在,则好的,文件已找到消息。如果没有,则找不到文件

import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials

SCOPES = ['https://www.googleapis.com/auth/drive']    
def main():
    creds = None
    if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        with open('token.json', 'w') as token:
            token.write(creds.to_json())

    service = build('drive', 'v3', credentials=creds)
    filename = 'Image'
    page_token = None
    while True:
        response = service.files().list(q="name contains '"+filename+"'",
                                              spaces='drive',
                                              fields='nextPageToken, files(id, name)',
                                              pageToken=page_token).execute()
        for file in response.get('files', []):
            # Process change
            print('Found file: %s (%s)' % (file.get('name'), file.get('id')))
        page_token = response.get('nextPageToken', None)
        if page_token is None:
            break

if __name__ == '__main__':
    main()

最佳答案

I want is to search for the files that are present in Google drive.

执行普通的 file.list 将返回 Google 驱动器根目录中的所有文件,没有特殊顺序。

I don't know where to give the folder id to find the files?.

要搜索特定文件夹中的所有文件,您需要使用 Q 搜索参数的 parents in 选项。

response = service.files().list(q="parents in '"+ Folder ID +"'",
                                              spaces='drive',
                                              fields='nextPageToken, files(id, name)',
                                              pageToken=page_token).execute()

我建议执行如下操作,首先搜索文件夹,这将帮助您找到可以在上面的请求中使用的文件夹的文件 ID。

response = service.files().list(q="name = '"+ folder name +"' and mimeType = 'application/vnd.google-apps.folder'",
                                                  spaces='drive',
                                                  fields='nextPageToken, files(id, name)',

您可能想查看Search for files and folders它有一些有趣的例子。

If they exist then okay, the files are found message. If not then files are not found

如果您正在搜索特定的文件名,并且该名称不存在,您可能会从 API 返回空响应而不是错误。因此,只需测试是否有任何文件返回即可告诉您是否找到了这些文件。

关于python - 如何在Google Drive中搜索特定文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66914793/

相关文章:

python - Openpyxl 跳过第一行

python - 如何让多个键绑定(bind)在 turtle 图形中同时工作?

python - 使用 API 创建 Google 共享联系人 - 联系人已创建,但不在共享目录中

python - 即使显式等待已经存在,也无法摆脱硬编码延迟

python - 更改字体大小时如何使条目小部件的标签位置正确更新?

javascript - 取消 Google API 请求

c# - 执行身份验证请求返回意外结果 : 404

python - ModuleNotFoundError : No module named 'tf_slim' despite installing

python - 如何在fiddler中捕获python https流量?

python - 身份验证在模块中不可用