flush and close the csv writer