Search code examples
pythonpython-3.xconfiguration-files

Rewriting configuration file at runtime


I'm using configuration files via configparser in python3.7.1.
I have a variable in one of the files that I would like to modify at runtime an was wondering what is the best way to do it.

Right now I rewrite the entire file as so:

config = configparser.ConfigParser()
config.read('config.ini')
config['Categorie']['new_variable'] = variable
with open('config.ini', 'w') as configfile:
    config.write(configfile)

I have 2 concerns with this method:

  • potentially losing the config on an error
  • deleting comments in the file each time

Solution

  • configparser doesn't store comments, so you're stuck on that one unless you don't use this module... Or you could extract the comments and reinject them in the file afterwards (losing the position of the comments, but keeping the contents)

    To avoid losing the configuration in case of an error (disk full or other) you could save as another name, delete the current file and rename.

    conf_file = 'config.ini'
    with open(conf_file+'.bak', 'w') as configfile:
        config.write(configfile)
    if os.path.exists(conf_file):
       os.remove(conf_file)  # else rename won't work when target exists
    os.rename(conf_file+'.bak',conf_file)
    

    this method is safe. If file cannot be written, the previous file isn't clobbered. Worst case is (if someone pulls the plug at the exact moment when the original file is removed) the .bak file (with proper new contents) remains.

    An alternative would be to rename existing .ini file, write the new one, and delete the old .ini file when the file has been successfully written.

    All operations are performed on the same drive, so there's no more disk access (except for the renaming), even if the file is huge.