Use context managers for file handles
This PR replaces all occurrences of explicit file opening and closing with context handlers. e.g. I've replaced
f = open(filename, 'r')
...
f.close()
with
with open(filename, 'r') as f:
...
which is apparently better practice. This is intended to close #10 (closed).
I also refactored the LEGACY output while I was it and added a test of utilities.sparse_print.