Reputation: 127
how are you doing?
I'm trying to rename some files in my Google Drive folder, following the instructions listed in documentation. However I'm getting an error in one of the arguments (FileNotFoundError: [Errno 2] No such file or directory: 'trying_new_filename_string')
I ONLY NEED TO RENAME THE DOCUMENT, NOTHING ELSE.
This is my code:
service = build('drive', 'v3', credentials=credentials)
file_id = '1TKxcYDzEK3SUjSv6dCMM6WkKRmZwm84SPWVhR1F2DEc'
new_title = 'trying_new_title'
new_mime_type = 'trying_new_mime_type'
new_filename = 'trying_new_filename_string'
from apiclient import errors
from apiclient.http import MediaFileUpload
def update_file(service, file_id, new_title, new_mime_type, new_filename):
# First retrieve the file from the API.
file = service.files().get(fileId=file_id).execute()
# File's new metadata.
file['name'] = new_title
file['mimeType'] = new_mime_type
# File's new content.
media_body = MediaFileUpload(
new_filename, mimetype=new_mime_type, resumable=True)
# Send the request to the API.
updated_file = service.files().update(
fileId=file_id,
body=file,
media_body=media_body).execute()
return updated_file
Upvotes: 3
Views: 2240
Reputation: 2462
Seems related to the fact the new_filename
does not exist. As the MediaUploadFile
tries to open and does not find a file with the trying_new_filename_string
name.
As you only need to rename that file, don't change its content, you should remove the MediaUploadFile
method.
Try:
def renameFile(service, fileId, newTitle):
body = {'name': newTitle}
return service.files().update(fileId=fileId, body=body).execute()
Upvotes: 3