Use `with open` when opening/writing files. PEP8 fixes.

Use `with open` when opening/writing files so it will automatically close the file, even if there is an exception. Applied whitespace PEP8 fixes to the files touched.

http://sdqali.in/blog/2012/07/09/understanding-pythons-with/
> The advantage of using a with statement is that it is guaranteed to close the file no matter how the nested block exits. If an exception occurs before the end of the block, it will close the file before the exception is caught by an outer exception handler. If the nested block were to contain a return statement, or a continue or break statement, the with statement would automatically close the file in those cases, too.