Search code examples
python-3.xgoogle-drive-apigoogle-api-python-client

how to search the files inside the folders on Google Drive using Python


I want to search the files inside the folder i.e. file from below path: test-download/abc/test.txt. But when tried the Google APIs It's giving me the separate files and folder name but How I come to know that the 'test.txt' is from 'test-download/abc'. Tried below code:

service = discovery.build('drive', 'v3', http=creds.authorize(Http()))
    page_token = None
    while True:
        response = service.files().list(q="mimeType='application/vnd.google-apps.folder' or mimeType='text/plain'",
                                              spaces='drive',
                                              fields='nextPageToken, files(id, name)',
                                              pageToken=page_token).execute()
        print(response)                                              
        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        

Solution

  • You can use the q query to specify the Id of the parent folder, e.g.

            response = service.files().list(q="'PASTE HERE THE PARENT FOLDER ID' in parents",
                                                  spaces='drive',
                                                  fields='nextPageToken, files(id, name)',
                                                  pageToken=page_token).execute()