Google驱动器文件夹ID

2024-04-20 04:15:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文件夹路径,例如/docs/word,我想获得“word”文件夹(最后一个文件夹)的ID,以便在那里上载文件。我怎么拿到身份证?在


Tags: 文件路径文件夹iddocsword身份证
1条回答
网友
1楼 · 发布于 2024-04-20 04:15:57

所以我想出来了。你要做的是获取根目录的id drive_service.about().get().execute()["rootFolderId"],然后在根目录下获取文件,转到路径中的下一个文件夹,等等。顺便说一句,我写的函数列出路径中的文件夹并将它们保存到字典中(使用自身地址路径())

def listFolders(self, path):
    fId = self.getPathId(path) #get the id of the parent folder
    files = self.drive_service.children().list(folderId=fId).execute() #Request children
    files = files["items"] #All of the items in the folder

    folders = []
    for i in range(len(files)):
        sId = files[i]["id"]
        sFile = self.drive_service.files().get(fileId=sId).execute()
        if sFile["labels"]["trashed"] == False and sFile["mimeType"] == "application/vnd.google-apps.folder":
            self.addPath(path+sFile["title"]+"/", sFile["id"])
            folders.append(sFile["title"])
    return folders

相关问题 更多 >