objectquery: simplify closing of file objects

This takes advantage of the file context manager (`with`) to simplify
closure of files. This patch removes several explicit `fp.close()`
statements.