0

The directory stricture on Google Drive is as follows: Inside mydrive/BTP/BTP-4

I need to get the folder ID for BTP-4 so that I can transfer a specific file from the folder. How do I do it?

fileList = GoogleDrive(self.driveConn).ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file in fileList:
    if (file['title'] == "BTP-4"):
        fileID = file['id']
        print(remoteFile, fileID)
        return fileID
Nimantha
  • 6,405
  • 6
  • 28
  • 69
hinata
  • 45
  • 1
  • 9
  • https://stackoverflow.com/questions/34101427/accessing-folders-subfolders-and-subfiles-using-pydrive-python this can help you. – Avinash Dalvi May 29 '20 at 18:34
  • It actually did but say I have the path of the file i need to download. Will be able to give path like /MyDrive/BTP/BTP-4 and filename as "test.csv" and then directly download the file? – hinata May 29 '20 at 19:05
  • Add your code which you tried other than this – Avinash Dalvi May 29 '20 at 19:06
  • for i, file1 in enumerate(sorted(file_list, key=lambda x: x['title']), start=1): if "." in file1['title']: try: print('Downloading {} from GDrive ({}/{})'.format(file1['title'], i, len(file_list))) file1.GetContentFile(os.path.join("C:\\Desktop", file1['title'])) except Exception as e: print("UNABLE TO TRANSFER FILES:" + str(e)) – hinata May 29 '20 at 19:12
  • Update in question – Avinash Dalvi May 29 '20 at 19:13

1 Answers1

2

Will be able to give path like /MyDrive/BTP/BTP-4 and filename as "test.csv" and then directly download the file?

Answer:

Unfortunately, this is not possible.

More Information:

Google Drive supports creating multiple files or folders with the same name in the same location:

enter image description here

As a result of this, in some cases, providing a file path isn't enough to identify a file or folder uniquiely - in this case mydrive/Parent folder/Child folder/Child doc points to two different files, and mydrive/Parent folder/Child folder/Child folder points to five different folders.

You have to either directly search for the folder with its ID, or to get a folder/file's ID you have to search for children recursively through the folders like you are already doing.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54