Showing results for 
Show  only  | Search instead for 
Did you mean: 
Community Participant

Resetting a course with canvasapi

I'm trying my hand at a python script that will reset a batch of courses using the canvasapi, and I want to attempt it with the following:

import csv
import sys
from canvasapi import Canvas
API_URL = ""
API_KEY = <your api key here>
canvas = Canvas(API_URL, API_KEY)

with open('H:\TESTING RESULTS\cnvs_200227_050011_courses.csv') as csvDataFile:
csvReader = csv.reader(csvDataFile)
for row in csvReader:
courseId = row[0]
partedCourse = courseId.rpartition("-")
term = partedCourse[2]
print(courseId, term)
if term == '21/SP' or term == '21/IN' or term == '20/FA':
course = canvas.get_course(courseId)

I've confirmed that I can successfully read the records from the csv file, and accurately parse out the course ID and term. But can someone confirm that the last 2 lines will perform the desired task?

I'm a total noob at both python and working with the CanvasAPI, so any other suggestions are also welcome.


Tags (2)
7 Replies
Community Champion

Greetings  @swaldie ,

Yes, that command will perform the same course reset that is in the Settings of a course. You may want to change that last line to something like this so you can grab the new course ID:

new_course = course.reset()
Community Participant

Thanks, Matt!

I appreciate your feedback and the quick response.

Quick the highlighted variable on line 1 actually spose to be the same as that on line 2:


Community Champion

 @swaldie ,

So when you perform a course reset using that first line, you will get a response object back. The new_course variable will store that response object and then you can see the new course ID using that second line. A course reset actually creates a new empty course with a different course ID and deletes the old course so you'll need that new course ID if you want to do anything like enroll users through the API (instead of using SIS imports). Hopefully I'm explaining that correctly as well as interpreting your question correctly.

Community Champion

Reading your question over again, I would also like to say that you could do this:


And that would print the entire object that was returned by the reset. Usually you only need the new course ID so the would only show the ID value instead of the whole object.

Community Participant

Ah yes, I get it now. Specifying the ".Id" singles out the course ID part of new_course.

Thanks again, Matt! This has been very helpful.


Hey  @swaldie ‌,

This doesn't answer your question, but I highly recommend trying out your developmental scripts on the test instance of Canvas - which is designed exactly for this type of thing. 

Community Participant

Thanks, Danny. I'll definitely be testing this out in the test env first.