如何列出特定 google 驱动器目录 Python 中的所有文件
How to list all the files in a specific google drive directory Python
按文件夹 ID 列出特定 google 驱动器目录的所有文件的最佳方法是什么。如果我构建如下所示的服务,下一步是什么?找不到对我有用的东西。 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)
file.list 方法有一个名为 q 的参数。您可以使用 q 到 search 来表示目录中的文件。
假设您知道您正在查找的文件夹的文件 ID,您将执行“parents in folderid”
这将 return 该文件夹中的所有文件。
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
我相信你的目标如下。
- 您想使用 python 的服务帐户检索特定文件夹下的文件列表。
在这种情况下,我想提出以下2种模式。
模式 1:
在此模式中,使用了 python 的 Drive API 中的“文件:列表”方法和 googleapis。但在这种情况下,特定文件夹的子文件夹中的文件不会被检索。
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"
表示在topFolderId
. 文件夹下获取文件列表
- 使用
pageSize=1000
时,可以减少Drive API的使用次数。
模式二:
在这个模式中,使用了getfilelistpy的库。在这种情况下,还可以检索特定文件夹中的子文件夹中的文件。首先,请按如下方式安装库。
$ pip install getfilelistpy
示例脚本如下
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))
- 在这个库中,可以使用 Drive API 中的“文件:列表”方法搜索特定文件夹中的子文件夹,使用 googleapis for python。
参考文献:
按文件夹 ID 列出特定 google 驱动器目录的所有文件的最佳方法是什么。如果我构建如下所示的服务,下一步是什么?找不到对我有用的东西。 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)
file.list 方法有一个名为 q 的参数。您可以使用 q 到 search 来表示目录中的文件。
假设您知道您正在查找的文件夹的文件 ID,您将执行“parents in folderid”
这将 return 该文件夹中的所有文件。
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
我相信你的目标如下。
- 您想使用 python 的服务帐户检索特定文件夹下的文件列表。
在这种情况下,我想提出以下2种模式。
模式 1:
在此模式中,使用了 python 的 Drive API 中的“文件:列表”方法和 googleapis。但在这种情况下,特定文件夹的子文件夹中的文件不会被检索。
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"
表示在topFolderId
. 文件夹下获取文件列表
- 使用
pageSize=1000
时,可以减少Drive API的使用次数。
模式二:
在这个模式中,使用了getfilelistpy的库。在这种情况下,还可以检索特定文件夹中的子文件夹中的文件。首先,请按如下方式安装库。
$ pip install getfilelistpy
示例脚本如下
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))
- 在这个库中,可以使用 Drive API 中的“文件:列表”方法搜索特定文件夹中的子文件夹,使用 googleapis for python。