How to search Google Drive with Google Colaboratory

Preparing to use pydrive

Authentication code creation


from google.colab import auth
auth.authenticate_user()

Preparation for drive operation with pydrive


from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from oauth2client.client import GoogleCredentials

gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

Please refer to the following when executing without authentication work. https://qiita.com/plumfield56/items/3d9e234366bcaea794ac

retrieval method

Once you are ready for the above state, run the search below.

python


drive.ListFile({'q':"{Describe the contents of query}"})

Search example

For example, Title is Hello and mimeType is Spreadsheet, Other than Trash. When searching, describe as follows.

python


drive.ListFile({'q':"title='Hello' and mimeType='application/vnd.google-apps.spreadsheet' and trashed = false"})

For other search examples, refer to the following site https://developers.google.com/drive/api/v2/search-files#query_string_examples

Use the following to make detailed specifications with query.

How to specify query

the term Available comparison operators how to use
title contains, =, != Search for title
Escape with backspace when using single quotes
fullText contains Search for title, description, content, index
mimeType contains, =, != Search for MIME type
modifiedDate <=, <, =, !=, >, >= Search for last updated date
lastViewedByMeDate <=, <, =, !=, >, >= Search by last viewed date
trashed =, != Specify whether to search in the trash(true,Specified by false)
starred =, != Specify whether to search for items with favorites(true,Specified by false)
parents in Specify parent ID
owners in Specify owner
writers in Specifies whether a particular user is an editor
readers in Specify whether a specific user is a viewer

See the site below https://developers.google.com/drive/api/v2/ref-search-terms

List of MIME Types of Google Services

Service name MIME Types
Google Drive file application/vnd.google-apps.file
Google Drive folder application/vnd.google-apps.folder
Google Docs application/vnd.google-apps.document
Google Sheets application/vnd.google-apps.spreadsheet
Google Slides application/vnd.google-apps.presentation
Google Sites application/vnd.google-apps.site
Google Forms application/vnd.google-apps.form
Google Drawing application/vnd.google-apps.drawing
Google Fusion Tables application/vnd.google-apps.fusiontable
Google My Maps application/vnd.google-apps.map
Google Apps Scripts application/vnd.google-apps.script
Shortcut application/vnd.google-apps.shortcut
application/vnd.google-apps.photo

See the site below https://developers.google.com/drive/api/v2/mime-types

List of MIME Types other than Google services

file format MIME Types
PDF application/pdf
JSON application/vnd.google-apps.script+json
Plain text text/plain
HTML text/html
MS Excel application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
MS PowerPoint application/vnd.openxmlformats-officedocument.presentationml.presentation
MS Word document application/vnd.openxmlformats-officedocument.wordprocessingml.document
JPEG image/jpeg
PNG image/png
SVG image/svg+xml
JSON application/vnd.google-apps.script+json

See the site below https://developers.google.com/drive/api/v2/ref-export-formats

Get data from search results

Use the GetList method to list the contents specified by query and extract the desired data.

Get data from search results


file_list = drive.ListFile({'q':"title='Hello' and mimeType='application/vnd.google-apps.spreadsheet' and trashed = false"}).GetList()

for file in file_list:
  #File/Contents you want to extract from the folder
  print(file['title'], file['id'])

List of items that can be extracted

Property name type Description
id string File ID
selfLink string File URL
title string file name
mimeType string File mimeType

There are many other items that can be extracted, so see below for details. https://developers.google.com/drive/api/v2/reference/files

Recommended Posts

How to search Google Drive with Google Colaboratory
How to load files in Google Drive with Google Colaboratory
How to use Google Colaboratory
How to analyze with Google Colaboratory using Kaggle API
Upload images to Google Drive with Python
Download files directly to Google Drive (using Google Colaboratory)
Upload files to Google Drive with Lambda (Python)
How to use search sorted
Study Python with Google Colaboratory
How to update with SQLAlchemy?
How to cast with Theano
How to Alter with SQLAlchemy?
How to separate strings with','
Mount google drive with google-drive-ocamlfuse
Access Google Drive with Python
How to RDP with Fedora31
Try OpenCV with Google Colaboratory
Upload to a shared drive with Google Drive API V3
How to use Spacy Japanese model in Google Colaboratory
How to Delete with SQLAlchemy?
Building an environment to use CaboCha with google colaboratory
How to extract any appointment in Google Calendar with Python
How to write to update Datastore to async with Google Apps Engine
How to deal with UnicodeDecodeError when executing google image download
Try to implement linear regression using Pytorch with Google Colaboratory
How to cancel RT with tweepy
[Rails] How to display Google Map
How to use virtualenv with PowerShell
How to install python-pip with ubuntu20.04LTS
How to deal with imbalanced data
Output cell to file with Colaboratory
How to get started with Scrapy
How to get started with Python
[Memo] How to use Google MµG
How to deal with DistributionNotFound errors
How to get started with Django
How to Data Augmentation with PyTorch
How to use FTP with Python
OpenCV feature detection with Google Colaboratory
How to calculate date with python
How to use bing search api
How to install mysql-connector with pip3
How to INNER JOIN with SQLAlchemy
How to install Anaconda with pyenv
How to authenticate with Django Part 2
How to authenticate with Django Part 3
Run the program without building a Python environment! !! (How to get started with Google Colaboratory)
How to display formulas in latex when using sympy (> = 1.4) in Google Colaboratory
Google colaboratory
How to use Google Colaboratory and usage example (PyTorch x DCGAN)
How to connect to Cloud Firestore from Google Cloud Functions with python code
How to do arithmetic with Django template
[Blender] How to set shape_key with script
How to title multiple figures with matplotlib
[Rails] How to introduce Google Analytics [Easy]
How to get parent id with sqlalchemy
How to set optuna (how to write search space)
How to add a package with PyCharm
How to update Google Sheets from Python
How to install DLIB with 2020 / CUDA enabled
How to use ManyToManyField with Django's Admin