File listing made easy

PyDrive2 handles pagination and returns GoogleDriveFile instances.

List matching files

Pass the files.list query parameters to ListFile() as a dictionary. It returns a GoogleDriveFileList. GetList() fetches all matches when you omit pageSize. If you set pageSize, each call fetches one page.

Use Drive v3 names such as name and modifiedTime in the file search guide. Request specific fields with a field mask, for example {"fields": "files(id,name,parents)"}. PyDrive2 requests the next page token automatically so field selection preserves pagination.

from pydrive2.drive import GoogleDrive

# Create GoogleDrive instance with authenticated GoogleAuth instance
drive = GoogleDrive(gauth)

# 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('name: %s, id: %s' % (file1['name'], file1['id']))

To update a returned file, include id in any custom field mask, such as files(id,name). The default listing already includes the ID.

Paginate and iterate through files

To process one page at a time, iterate over the result of ListFile(). In this example, each page contains up to ten files.

for file_list in drive.ListFile({'q': 'trashed=false', 'pageSize': 10}):
    print('Received %s files from Files.list()' % len(file_list))  # <= 10
    for file1 in file_list:
        print('name: %s, id: %s' % (file1['name'], file1['id']))