Skip to content

Instantly share code, notes, and snippets.

@sborquez
Last active January 12, 2020 03:16
Show Gist options
  • Save sborquez/3dbf1f2a22e8ef9409eaf14cd4a2a0cc to your computer and use it in GitHub Desktop.
Save sborquez/3dbf1f2a22e8ef9409eaf14cd4a2a0cc to your computer and use it in GitHub Desktop.
Moodle utils scripts
import os
import glob
from tqdm import tqdm
from collections import Counter
def rename_submissions(submissions_folder, output_folder=None):
"""
Rename submissions downloaded from Moodle (download all submissions option).
Use it when the name of the submited files are differents between each student
and contains identification info in it.
"""
assert os.path.exists(submissions_folder), "invalid submissions_folder"
assert (output_folder is None) or os.path.exists(output_folder), "invalid output_folder"
if output_folder is None:
output_folder = submissions_folder
counter = Counter()
submissions = glob.glob(os.path.join(submissions_folder, "*_assignsubmission_file_*"))
if len(submissions) == 0:
print("Folder doesn't contains submissions.")
return
for submission in tqdm(submissions):
new_filename = submission.split("_assignsubmission_file_")[1]
counter[new_filename] += 1
if counter[new_filename] > 1:
filename, ext = os.path.splitext(new_filename)
new_filename = f"{filename}_(copy {counter[new_filename]}){ext}"
os.rename(submission, os.path.join(output_folder, new_filename))
for f, c in filter((lambda x: x[1] > 1), counter.items()):
print(f"warning: {f} has {c} copies")
if __name__ == "__main__":
import sys
if len(sys.argv) == 2:
rename_submissions(sys.argv[1])
elif len(sys.argv) == 3:
rename_submissions(sys.argv[1], sys.argv[2])
else:
print("rename_submissions.py submissions_folder [output_folder]")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment