Add try...finally block to close parsed files

- Add try...finally block around file opening and parsing code to ensure
  that file is closed whether errors are raised or not. Before, only
  non-gzip files would get closed.

This commit differs from commit 1aa8572d46a1822a58baa6188a401886d37898af
in that the parse_file call is kept for both cases (gzip/non-gzip). This
is because the gzip module has slightly inconsistent behaviour compared to
Python file-like objects where opening a non-gzip file doesn't raise an
exception, only an attempt to then read that file does.