I'd rather see a list of strings being built, and at the end, just `writelines()` those into the file.