以下是使用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()