File listing made easy¶
PyDrive handles paginations and parses response as list of GoogleDriveFile.
Get all files which matches the query¶
Create GoogleDriveFileList instance with parameters of Files.list() as dict
.
Call GetList() and you will get all files that matches your query as a list of GoogleDriveFile.
The syntax and possible option of the query q
parameter can be found in search for files Google documentation.
from pydrive2.drive import GoogleDrive
drive = GoogleDrive(gauth) # Create GoogleDrive instance with authenticated GoogleAuth instance
# Auto-iterate through all files in the root folder.
file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file1 in file_list:
print('title: %s, id: %s' % (file1['title'], file1['id']))
You can update metadata or content of these GoogleDriveFile instances if you need it.
Paginate and iterate through files¶
PyDrive provides Pythonic way of paginating and iterating through list of files.
Here is an example how to do this, maxResults
below defines how many
files it retrieves at once and we wrap it into a for
loop to iterate:
Sample code continues from above:
# Paginate file lists by specifying number of max results
for file_list in drive.ListFile({'q': 'trashed=true', 'maxResults': 10}):
print('Received %s files from Files.list()' % len(file_list)) # <= 10
for file1 in file_list:
print('title: %s, id: %s' % (file1['title'], file1['id']))