www*_*139 1 python google-api google-drive-api google-api-python-client
在使用 Google Drive API 获取文件列表时,我试图找到文件的路径。
现在,我能够获取文件属性(目前只获取校验和、id、名称和 mimeType):
results = globalShares.service.files().list(pageSize=1000,corpora='user',fields='nextPageToken, files(md5Checksum, id, name, mimeType)').execute()
items = results.get('files',[])
nextPageToken = results.get('nextPageToken',False)
for file in items:
print("===========================================================")
pp.pprint(file)
print(str(len(items)))
print(nextPageToken)
Run Code Online (Sandbox Code Playgroud)
列表文档(提供给 list() 方法的参数)
文件文档(每个文件返回的属性)
如果我的理解是正确的,这个示例脚本怎么样?遗憾的是,在当前阶段,Google API 无法直接检索文件的文件夹树。所以需要准备一个脚本来实现它。请将此视为几个答案之一。
此示例脚本检索文件的文件夹树。使用本脚本时,请设置文件ID。
fileId = '###' # Please set the file ID here.
tree = [] # Result
file = globalShares.service.files().get(fileId=fileId', fields='id, name, parents').execute()
parent = file.get('parents')
if parent:
while True:
folder = service.files().get(
fileId=parent[0], fields='id, name, parents').execute()
parent = folder.get('parents')
if parent is None:
break
tree.append({'id': parent[0], 'name': folder.get('name')})
print(tree)
Run Code Online (Sandbox Code Playgroud)
在文件为三层结构的情况下,运行脚本时,返回如下对象。
[
{
"id": "folderId3",
"name": "folderName3"
},
{
"id": "folderId2",
"name": "folderName2"
},
{
"id": "folderId1",
"name": "My Drive" # This is the root folder.
}
]
Run Code Online (Sandbox Code Playgroud)
parents. 请注意这一点。pinoyyid的评论也提到了这一点。| 归档时间: |
|
| 查看次数: |
6164 次 |
| 最近记录: |