Don't append for first iteration

The first iteration of the appending function should be a write operation to overwrite the previous output file.