Added error handling for file I/O

Surrounded the with open() as file: blocks in a try/catch block, and
slightly tweaked some of the header comments at the top of the file.