Search code examples
pythongoogle-drive-apipydrive

pydrive get only folders from list


I use library pydrive for google drive. Now I need get all folders (root, parent, child) name in google drive.
I have found this:

self.drive.ListFile().GetList()

but it returns all the files. Can I get only all folders in google drive?


Solution

  • How about this modification? It retrieves the files with mimeType of application/vnd.google-apps.folder using query.

    • The mimeType of application/vnd.google-apps.folder means folders.
    • trashed=false means that files are retrieved from outside of the trashbox.
    • In this case, title means folder name.

    Modified script :

    f = self.drive.ListFile({"q": "mimeType='application/vnd.google-apps.folder' and trashed=false"}).GetList()
    for folder in f:
        print(folder['title'])
    

    References :

    If I misunderstand your question, I'm sorry.