6

I have been trying to move file from one folder to another in my google drive using PyDrive

file2 = drive.CreateFile({'id': <file id>})
file2['parents']=<destination folder id>
file2.Upload()

Is it possible to change folders like this? If not please suggest any alternatives using PyDrive.

gooner
  • 61
  • 1
  • 4
  • I don't know the library you are using, but what should work is to include the parents array in the original create, eg. {'title'": "myfile", parents[{'id':"destination_folder_id}]}. As an aside, it's a bit odd to see you creating a file with a given ID. It's more common to see a file created with a given name/title. - COYS!!!! – pinoyyid Apr 18 '17 at 22:26
  • 1
    Hi, thanks for the reply. I am trying to move an existing file from google drive. I believe, from the documentation of PyDrive [link] (https://pythonhosted.org/PyDrive/filemanagement.html) here that, in order to access and change an existing file, you use the same CreateFile feature, and modify the attributes. I tried doing the same for title and it worked. But not for changing parents. --#Wengerout – gooner Apr 19 '17 at 05:07
  • 1
    cant help with that library but the API supports it using update I believe https://developers.google.com/drive/v3/reference/files/update – Linda Lawton - DaImTo Apr 19 '17 at 07:34
  • @gooner, you are right to call CreateFile with the existing ID in order to update its metadata. Adding the parent ID to the array of parents is also conceptually correct. Are you sure that you are using the correct parent ID? – Robin Nabel Apr 19 '17 at 14:03

5 Answers5

6

It's been a while since this question was asked, but for those that happen upon this, here's what worked for me. The solutions above didn't work for me, but what did was that I changed 'kind': 'drive#fileLink' to 'drive#parentReference'.

Final code looks like;

file2 = drive.CreateFile({'id': <file id>})
file2['parents'] = [{"kind": "drive#parentReference", "id": <destination_id>}]
file2.Upload()

This will overwrite the parent info for the file, effectively emulating the 'Move To' function in the Drive UI.

HaplessEcologist
  • 375
  • 3
  • 11
  • What a lifesaver, thanks a lot! I was trying with the `.UpdateMetadata()` method but nada, only this approach works. There should be a method to move files which abstracts this stuff away IMO. – Manchineel Jul 04 '22 at 17:08
1

PyDrive is a convenient wrapper for the Google Drive api, however it seems there are still some methods that don't work as expected.

Fortunately you can still access the underlying google drive api methods proposed here like so:

file_id   = <file id>
new_parent = <new parent id>

files = drive.auth.service.files()
file  = files.get(fileId= file_id, fields= 'parents').execute()
prev_parents = ','.join(p['id'] for p in file.get('parents'))
file  = files.update( fileId = file_id,
                      addParents = new_parent,
                      removeParents = prev_parents,
                      fields = 'id, parents',
                      ).execute()

I just tested this, and if you're using the web UI, you will need to refresh the pages to see changes.

Note that a single file can have multiple parents, which can be pretty useful for things like jQuery galleries.

0

The parents array contains not just the IDs of each parent folder but an object with additional information.

Thus, you need to change the line:

file2['parents'] += ['<parent ID>']

to:

file2['parents'].append({"kind": "drive#fileLink", "id": '<parent ID>'})

The complete example would therefore be:

file2 = drive.CreateFile({'id': <file id>})
file2['parents'].append({"kind": "drive#fileLink", "id": '<parent ID>'})
file2.Upload()

You can find an example of using folders here.

Robin Nabel
  • 2,170
  • 1
  • 21
  • 26
  • 1
    Thanks for the reply. I just tried doing this. It hasnt moved, but showed that the file has been edited. It is still not in the destination folder. Even when I am trying to rename the file, it using `file2['title']=` it just shows up on the drive that file has been edited but title was not changed. Is there any time delay for the change to get reflected on the drive? – gooner Apr 20 '17 at 10:20
  • Title change actually worked once. Not every time I try doing it. – gooner Apr 20 '17 at 10:53
  • Hm. The code above should only add your file to the new folder - it will currently not remove it from the first folder (it is appending the new folder ID, if you want to move it instead do this: `file2['parents'] = [{"kind": "drive#fileLink", "id": ''}]`). You can check whether that works for you. Could you check whether reloading the tab after you rename the file helps? Changing the title should definitely work. – Robin Nabel Apr 20 '17 at 12:23
0

what actually worked :-

file1 = drive.CreateFile({'id': id_[0]})
file1.Upload()
file1['parents'] = [{"kind": "drive#parentReference", "id": "destination_folder_id"}]
file1.Upload()

Explanation:-

line 1:-

file1 = drive.CreateFile({'id': 'file_id_here'}) #Create's GoogleDriveFile instance

the above line of code doesn't imports the meta data from your file in order to do that we use:-

line 2:-

file1.Upload() #gets metadata of the file 

and once meta data is retrived we change the parent folder id to the destination folder id in line 3

line 3:-

file1['parents'] = [{"kind": "drive#parentReference", "id": "destination_folder_id"}]

now upload it once again to save the changes (line 4), line 4:-

file1.Upload()# save and update changes
0

Here's the link to github repo with the working code.