简体   繁体   中英

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. 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.

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. you can use the q to search for things like files with in a directory.

Assuming you know the file id of the folder you are looking it you would do "parents in 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.

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. 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 .
  • When pageSize=1000 is used, the number of use of Drive API can be reduced.

Pattern 2:

In this pattern, a library of getfilelistpy is used. 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.

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM