简体   繁体   English

如何列出特定谷歌驱动器目录 Python 中的所有文件

[英]How to list all the files in a specific google drive directory Python

What is the best way to list all the files of a specific google drive directory by folder ID.按文件夹 ID 列出特定谷歌驱动器目录的所有文件的最佳方法是什么。 If I build a service like below, what is the next step?如果我构建如下所示的服务,下一步是什么? COuldn't findanything that worked for me.找不到任何对我有用的东西。 Service_Account_File in thsi example is a json file with tokens.此示例中的 Service_Account_File 是带有令牌的 json 文件。

SCOPES = ['https://www.googleapis.com/auth/drive']
SERVICE_ACCOUNT_FILE = service_account_file 
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = discovery.build('drive', 'v3', credentials=credentials)

The file.list method has a parameter called q. file.list 方法有一个名为 q 的参数。 you can use the q to search for things like files with in a directory.您可以使用 q 搜索目录中的文件等内容。

Assuming you know the file id of the folder you are looking it you would do "parents in folderid"假设您知道正在查找的文件夹的文件 ID,您将执行“folderid 中的父母”

This will return all the files within that folder.这将返回该文件夹中的所有文件。

page_token = None
while True:
    response = drive_service.files().list(q="parents in 'YOURFOLDERIDHERE'",
                                          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

I believe your goal as follows.我相信你的目标如下。

  • You want to retrieve the file list under the specific folder using the service account with python.您想使用 python 的服务帐户检索特定文件夹下的文件列表。

In this case, I would like to propose the following 2 patterns.在这种情况下,我想提出以下两种模式。

Pattern 1:模式一:

In this pattern, the method of "Files: list" in Drive API with googleapis for python is used.在此模式中,使用 python 的 googleapis 驱动器 API 中的“文件:列表”方法。 But in this case, the files in the subfolders in the specific folder are not retrieved.但在这种情况下,不会检索特定文件夹中子文件夹中的文件。

from google.oauth2 import service_account
from googleapiclient.discovery import build

SCOPES = ['https://www.googleapis.com/auth/drive']
SERVICE_ACCOUNT_FILE = service_account_file
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('drive', 'v3', credentials=credentials)

topFolderId = '###' # Please set the folder of the top folder ID.

items = []
pageToken = ""
while pageToken is not None:
    response = service.files().list(q="'" + topFolderId + "' in parents", pageSize=1000, pageToken=pageToken, fields="nextPageToken, files(id, name)").execute()
    items.extend(response.get('files', []))
    pageToken = response.get('nextPageToken')

print(items)
  • q="'" + topFolderId + "' in parents" means that the file list is retrieved just under the folder of topFolderId . q="'" + topFolderId + "' in parents"表示在topFolderId的文件夹下检索文件列表。
  • When pageSize=1000 is used, the number of use of Drive API can be reduced.使用pageSize=1000时,可以减少Drive API的使用次数。

Pattern 2:模式二:

In this pattern, a library of getfilelistpy is used.在此模式中,使用了一个getfilelistpy库。 In this case, the files in the subfolders in the specific folder can be also retrieved.在这种情况下,也可以检索特定文件夹中子文件夹中的文件。 At first, please install the library as follows.首先,请按如下方式安装库。

$ pip install getfilelistpy

The sample script is as follows.示例脚本如下。

from google.oauth2 import service_account
from getfilelistpy import getfilelist

SCOPES = ['https://www.googleapis.com/auth/drive']
SERVICE_ACCOUNT_FILE = service_account_file
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)

topFolderId = '###' # Please set the folder of the top folder ID.
resource = {
    "service_account": credentials,
    "id": topFolderId,
    "fields": "files(name,id)",
}
res = getfilelist.GetFileList(resource)
print(dict(res))
  • In this library, the subfolders in the specific folder can be searched using the method of "Files: list" in Drive API with googleapis for python.在这个库中,可以使用驱动器API中的“文件:列表”的方法搜索特定文件夹中的子文件夹,googleapis for python。

References:参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google drive api python - 如何将文件夹或文件夹中的所有文件下载到特定的本地目标路径 - Google drive api python - How to download all the files inside a folder or the folder to a specific local destination path 如何删除谷歌驱动器中具有特定扩展名的所有文件 - how to delete all files with a specific extension in google drive Python:如何使用 folderId 将文件上传到 Google Drive 中的特定文件夹 - Python: How to upload files to a specific folder in Google Drive using folderId 如何列出Google云端硬盘文件夹的所有文件,文件夹,子文件夹和子文件 - How to list all files, folders, subfolders and subfiles of a Google drive folder Google Drive python 客户端/如何过滤具有“ownedByMe”和“属性”的文件列表? - Google Drive python client / How to filter list of files with 'ownedByMe' and 'properties'? 使用python的谷歌驱动器文件夹中的文件列表 - List of files in a google drive folder with python 如何在谷歌驱动器中搜索特定文件? - How to search for specific files in google drive? 如何使用 Python 在 Google Drive 上下载目录? - How to Download a directory on Google Drive using Python? 如何在Python中的目录中查找具有特定文件扩展名的所有文件 - How to find all files with specific file extensions in a directory in Python Python:如何在特定条件下读取目录中的所有文件? - Python : How to read all files in the directory with some specific condition?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM