I think one could use Python's `ConfigParser` class here to do the writing instead of assembling the string manually?