简体   繁体   中英

How to search for specific files in google drive?

I took this code from the Google developer website, made some changes to it. But I want is to search for the files that are present in Google drive. I don't know where to give the folder id to find the files?. If they exist then okay, the files are found message. If not then files are not found

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.

Doing a normal file.list will return all the files in the root of your google drive in no special order.

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

To search for all files within a specific folder, You will need to use the parents in option of the Q search parameter.

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

I recommend doing something like the following which would first search for the folder and this will help you find the file id of the folder which you can use th in the request above.

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

You might want to check out Search for files and folders it has some intersting examples.

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

If you are searching for a specific file name, and that name does not exist you are probably going to get an empty response back from the API not an error. So simply testing that there are any files returned will tell you if the files are found or not.

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