2
我有一個文件夾路徑,例如/ docs/word,我想獲得「word」文件夾的ID(最後一個文件夾),以便在那裏上傳文件。我如何獲得ID?谷歌雲端硬盤文件夾ID
我有一個文件夾路徑,例如/ docs/word,我想獲得「word」文件夾的ID(最後一個文件夾),以便在那裏上傳文件。我如何獲得ID?谷歌雲端硬盤文件夾ID
所以我想通了。你需要做的是獲得根drive_service.about().get().execute()["rootFolderId"]
的ID,然後獲取根目錄中的文件,轉到路徑中的下一個文件夾等等。btw,我寫的函數列出路徑中的文件夾並將它們保存到字典(使用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