I'm trying to find the path of a file when fetching a file list with the Google Drive API.
Right now, I'm able to fetch file properties (currently only fetching checksum, id, name, and 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)
List documentation (parameters fed to the list() method)
Files documentation (properties returned with each file)
If my understanding is correct, how about this sample script? Unfortunately, in the current stage, the folder tree of the file cannot directly be retrieved by the Google API. So it is required to prepare a script for achieving it. Please think of this as just one of several answers.
This sample script retrieves the folder tree of the file. When you use this script, please set the file 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)
In the case that the file has the three-layer structure, when you run the script, the following object is returned.
[
{
"id": "folderId3",
"name": "folderName3"
},
{
"id": "folderId2",
"name": "folderName2"
},
{
"id": "folderId1",
"name": "My Drive" # This is the root folder.
}
]
parents
. Please be careful this. This is also mentioned by pinoyyid's comment .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.