Use with-statement to avoid leaking file-descriptors.

Open files in context managers so that file-descriptors are automatically closed if any exceptions should occur.

I also removed some extraneous white-space etc.