Moved the write to after the "if file doesn't exist" statement