Help Renaming a Remote File #599
-
|
Suppose I have a file called foo.pdf stored in course files. How do I rename it to bar.pdf? I know how to copy the file, but I only want to change the file's display_name property. Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
This looks like a gap in the
from canvasapi import Canvas
canvas = Canvas(URL, KEY)
# Getting the file this way allows you to confirm you have the right one
# before moving on with the rename. If it's part of a larger script, you
# can also use the included methods on the File object in the library.
# https://canvasapi.readthedocs.io/en/stable/file-ref.html#canvasapi.file.File
file = canvas.get_file(123456)
updated_file = file._requester.request("PUT", "files/{}".format(file.id), name="New display name")
updated_file.json() # should have updated dataThe benefit of this method is that you can confirm which file you're updating before actually sending the update.
Since you need the file ID before making the update, you can also just create a standalone from canvasapi.requester import Requester
requester = Requester(URL, KEY)
updated_file = requester.request("PUT", "files/123456", name="New display name")
updated_file.json() # updated informationYou have to build then request manually either way, so depending on your context, this may be cleaner for one-off renames. |
Beta Was this translation helpful? Give feedback.
This looks like a gap in the
Fileobject in the library. There are a couple ways you can do this:Canvasmodule to first get the file and then update:The b…