Close output file if it was opened

Signed-off-by: Tomas Hozza <thozza@redhat.com>