Skip to content

Instantly share code, notes, and snippets.

@RyanNutt
Last active November 2, 2018 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyanNutt/363baaf23a38f63a4389331e98535ea3 to your computer and use it in GitHub Desktop.
Save RyanNutt/363baaf23a38f63a4389331e98535ea3 to your computer and use it in GitHub Desktop.
Move Canvas files from download package to individual folders for each student and keep original filenames . https://compsci.rocks

Move Canvas Files

Canvas is good for a lot of things, but turning in source code in a computer science class is not one of them.

Our district requires us to use Canvas, and for the most part that's a good thing. But when students need to turn in Java files there's a pretty significant issue. When you download submitted files they've all been renamed and stored in the root of the zip file.

Since they're Java files, they need to be named the same thing as the class which breaks by renaming the files

A Bit of Python

Yesterday I finally got frustrated enough to kick up a little Python script to fix the problem. It's the move_files.py file that's attached to this Gist.

What it does is find any file in the current directory that matches the regex [a-z]*?_[0-9]*?_[0-9]*?_ and then splits on the underscores to break out the pieces. First part is the student name, and is used for the folder name. Piece of the last underscore is the filename and what the file is renamed to as it's moved into the student's folder.

Usage

You'll need Python 3 somewhere in your path for this to work.

Copy the move_files.py file to the folder where you downloaded all of your students' work and then run the script. If you've installed Python you can probably just double click on it.

Updates & Quirks

I really need to make this work so that you don't have to copy the move_files.py file to every directory you want to fix.

The other issue that's come up is the way Canvas handles multiple submissions. If a student submits a file with the same name as a previous submission it tacks -1 on the end of the filename. That messes up Java when you try to run, and I probably should work out a way to strip that out. Best thing would probably be to read any file that ends with .java and look for the class name.

More About Me

And if you're interested, I blog about teaching computer science at CompSci.rocks.

"""
Moves files from a Canvas download into separate folders for
each user and renames the files to not have the leading junk
from Canvas
"""
from os import listdir
import os
import re
pattern = re.compile("[a-z]*?_[0-9]*?_[0-9]*?_");
for f in listdir('.'):
if pattern.match(f):
parts = f.split("_")
if not os.path.exists(parts[0]):
os.makedirs(parts[0])
os.rename(f, parts[0] + '/' + parts[3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment