I am dealing with cheaters on a programming exam and would like to cross-compare all answers from all candidates. I’ve arranged the files like so: 100.js 101.js 102.js etc.
How can I easily compare all these files with each other and find matches in similarity?
>Solution :
This is the strength of Python
import sys
import difflib
def read_file(filename):
try:
with open(filename, 'r') as f:
return f.readlines()
except IOError:
print("ERROR" % filename)
sys.exit(1)
def compare_file(file1, file2, out_file):
file1_content = read_file(file1)
file2_content = read_file(file2)
d = difflib.HtmlDiff()
result = d.make_file(file1_content, file2_content)
with open(out_file, 'w') as f:
f.writelines(result)
if __name__ == '__main__':
compare_file('100.js', '101.js', 'result.html')