Here I'd rather avoid unconditionally rewriting the config file. I'd rather set a flag 'changed = false' and set it to true when a config variable is changed and then write the file only when there was a change.

Rewriting the file always may change the indentation or the user comments (manually added) can be lost even when file update is not needed.