python - Google 云端硬盘文件夹 ID

标签 python python-2.7 google-drive-api

我有一个文件夹路径,例如/docs/word,我想获取“word”文件夹(最后一个文件夹)的 ID,以便在那里上传文件。如何获取 ID?

最佳答案

所以我想通了。你要做的就是获取root的id drive_service.about().get().execute()["rootFolderId"] ,然后获取root中的文件,进入下一步路径中的文件夹等等。顺便说一句,我编写的函数是列出路径中的文件夹并将它们保存到字典中(使用 self.addPath())

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

关于python - Google 云端硬盘文件夹 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15278315/

相关文章:

python - 计算time-1到time-2之间的时间?

python - 如何在多个 if/elif 语句中使用 while 循环?

python - 内存错误,被杀死

javascript - 定期从 Google Drive Trash 中删除特定类型的所有文件

google-apps-script - 驱动器 PDF 修订 ID 被忽略

使用 super() 时不调用 Python 多重继承构造函数

python - 在递归函数中获取扫描目录和文件的绝对路径?

python - 有没有一种方法可以在不运行 Python 脚本的情况下自动验证所有导入?

python - 使用 .iterrows() 以更简洁的方式遍历 pandas 数据框中的行,并跟踪特定值之间的行

google-app-engine - 如何搜索 md5 校验和?