List of file in a folder/ DRIVE API PyDRIVE

Below are two full working examples for printing Google drive files structure with pydrive - follow comments in the code.


Example 1 - basic usage of pydrive and printing top levels folders

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)

# 1) Choose your starting point by inserting file name
folder_title = "your-starting-point-folder"
folder_id = ''

# 2) Retrieve the folder id - start searching from root
file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file in file_list:
    if(file['title'] == folder_title):
        folder_id = file['id']
        break

# 3) Build string dynamically (need to use escape characters to support single quote syntax)
str = "\'" + folder_id + "\'" + " in parents and trashed=false"    

# 4) Starting iterating over files
file_list = drive.ListFile({'q': str}).GetList()
for file in file_list:
    print('title: %s, id: %s' % (file['title'], file['id']))

Example 2 - Recursively printing all file structure

I used a tree visualization library named treelib.

from treelib import Node, Tree

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)


### Some basic helper functions ### 
def get_children(root_folder_id):
    str = "\'" + root_folder_id + "\'" + " in parents and trashed=false"
    file_list = drive.ListFile({'q': str}).GetList()
    return file_list

def get_folder_id(root_folder_id, root_folder_title):
    file_list = get_children(root_folder_id)
    for file in file_list:
        if(file['title'] == root_folder_title):
            return file['id']

def add_children_to_tree(tree, file_list, parent_id):
    for file in file_list:
        tree.create_node(file['title'], file['id'], parent=parent_id)
        # For debugging
        # print('parent: %s, title: %s, id: %s' % (parent_id, file['title'], file['id']))

### Go down the tree until you reach a leaf ###
def populate_tree_recursively(tree,parent_id):
    children = get_children(parent_id)
    add_children_to_tree(tree, children, parent_id)
    if(len(children) > 0):
        for child in children:
            populate_tree_recursively(tree, child['id'])


### Create the tree and the top level node ###
def main():
    root_folder_title = "my-top-level-root-folder-name"
    root_folder_id = get_folder_id("root", root_folder_title)

    tree = Tree()
    tree.create_node(root_folder_title, root_folder_id)
    populate_tree_recursively(tree, root_folder_id)
    tree.show()

if __name__ == "__main__":
    main()

You have to insert the folder ID instead of its path. You can get the ID in different ways:

  1. Using PyDrive: If you list all folders in root, you can list all folder names with their respective IDs.
  2. Using the Web interface: Navigate into the folder you want to get the ID from. Look at the URL, it has this format: drive.google.com/drive/u/0/folders/<folder ID>

Now insert the folder ID into the request.

file_list = drive.ListFile({'q': "'<folder ID>' in parents and trashed=false"}).GetList()

FYI: Google Drive is a tag-based (also called semantic) file system, which, for example, allows a file to be in several places at the same time (just by adding IDs of folders to the file's parents property).