I have a folder path, for example /docs/word, and I would like to get the ID of the "word" folder (the last folder), in order to upload a file there. How do I get the ID?
So I figured it out. What you have to do is get the id of the root drive_service.about().get().execute()["rootFolderId"]
, and then get the files in root, go to the next folder in the path, etc. btw, the function i wrote to list the folders inside a path and save them to a dictionary (using 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With