Skip to content

Instantly share code, notes, and snippets.

@Cornelius-G
Created November 8, 2018 08:18
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 Cornelius-G/95e524ba8a56ddac57501c77aa758117 to your computer and use it in GitHub Desktop.
Save Cornelius-G/95e524ba8a56ddac57501c77aa758117 to your computer and use it in GitHub Desktop.
Python script to merge all questions downloaded from moodle into a single html file.
import os
import sys
import zipfile
# split string s at symbol c at the n-th delimeter
def split_at(s, c, n):
words = s.split(c)
return c.join(words[:n]), c.join(words[n:])
try:
zippath = sys.argv[1]
except IndexError:
cwd = os.getcwd()
zippath = cwd
try:
zip_ref = zipfile.ZipFile(zippath, 'r')
path = zippath.replace(".zip", "")
zip_ref.extractall(path)
zip_ref.close()
except:
pass
path = zippath
#set to the file extension of "to-be-merged" files
ext = '.html'
#set to the name of your output file
results = path + '/output.html'
files = os.listdir(path)
out = open(results, 'w+')
os.chdir(path)
for i, f in enumerate(files):
if f.endswith(ext) and f != results:
name = split_at(f, '_', 1)[0] # get name of student
data = open(f)
for l in data:
# do some html formatting
l = l.replace("<p>", "")
l = l.replace("</p>", "")
l = l.replace("<div>", "")
l = l.replace("</div>", "")
# print name + question
print("<p>", i+1, ".", name+":", l, "</p>", file=out)
data.close()
out.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment