Check if file exists before trying to write output