Open file only before writing