文件夹/ DRIVE API PyDRIVE中的文件列表

时间:2016-10-24 17:55:23

标签: python drive pydrive

我无法更改文件夹并查看其中的内容。

drive = GoogleDrive(gauth)
file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file1 in file_list:
print("File %s\n\n",(file1))

我使用了以下代码:

file_list = drive.ListFile({'q': "'/test1' in parents and trashed=false"}).GetList()

但它不起作用。 有我使用的函数的文档:https://developers.google.com/drive/v3/reference/files/list

4 个答案:

答案 0 :(得分:20)

您必须插入文件夹ID而不是其路径。您可以通过不同方式获取ID:

  1. 使用PyDrive:如果列出root中的所有文件夹,则可以列出所有文件夹名称及其各自的ID。
  2. 使用Web界面:导航到要从中获取ID的文件夹。查看网址,其格式为:drive.google.com/drive/u/0/folders/<folder ID>
  3. 现在将文件夹ID插入请求中。

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

    仅供参考:Google云端硬盘是基于标签的(也称为语义)文件系统,例如,它允许文件同时位于多个位置(只需将文件夹的ID添加到文件{{1属性)。

答案 1 :(得分:0)

我认为https://stackoverflow.com/a/47764444/1003629解决了类似的问题并应该对您有所帮助。这里的问题是所需文件夹的权限,默认情况下只有root。

答案 2 :(得分:0)

请参阅Google Drive API文档-https://developers.google.com/drive/api/v2/search-files

enter image description here

根据您的要求替换search_term:

示例:搜索具有名称的文件/文件夹

file_list = drive.ListFile({'q':“ title = ' '”})。GetList()

'单引号是api请求的一部分)

答案 3 :(得分:0)

下面是两个完整的工作示例,用于打印Google云端硬盘 pydrive的文件结构-请遵循代码中的注释。


示例1-pydrive的基本用法和打印顶级文件夹

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']))

示例2-递归打印所有文件结构

我使用了一个名为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()