Use context manager for output_file

Cc: @aconrad