15

我无法更改文件夹并查看里面的内容。

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

4 回答 4

42

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

  1. 使用 PyDrive:如果列出根目录下的所有文件夹,则可以列出所有文件夹名称及其各自的 ID。
  2. 使用 Web 界面:导航到要从中获取 ID 的文件夹。查看 URL,它具有以下格式:drive.google.com/drive/u/0/folders/<folder ID>

现在将文件夹 ID 插入到请求中。

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

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

于 2016-10-25T09:43:35.633 回答
4

以下是使用pydrive打印 Google 驱动器文件结构的两个完整工作示例- 请遵循代码中的注释。


示例 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()
于 2020-11-23T19:38:46.470 回答
0

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

于 2017-12-12T02:28:13.723 回答
0

请参考谷歌驱动 api 文档 - https://developers.google.com/drive/api/v2/search-files

在此处输入图像描述

根据您的要求替换 search_term :

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

file_list = drive.ListFile({'q': " title= ' <文件夹名称> ' "}).GetList()

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

于 2020-08-07T02:04:02.493 回答