Change value in ini file using ConfigParser Python Change value in ini file using ConfigParser Python python python

Change value in ini file using ConfigParser Python


As from the examples of the documentation:

https://docs.python.org/2/library/configparser.html

parser.set('SETTINGS', 'value', '15')# Writing our configuration file to 'example.ini'with open('example.ini', 'wb') as configfile:    parser.write(configfile)


I had an issue with:with open

Other way:

import configparserdef set_value_in_property_file(file_path, section, key, value):    config = configparser.RawConfigParser()    config.read(file_path)    config.set(section,key,value)                             cfgfile = open(file_path,'w')    config.write(cfgfile, space_around_delimiters=False)  # use flag in case case you need to avoid white space.    cfgfile.close()

It can be used for modifying java properties file: file.properties


Python's official docs on configparser illustrate how to read, modify and write a config-file.

import configparserconfig = configparser.ConfigParser()config.read('settings.ini')config.set('SETTINGS', 'value','15')with open('settings.ini', 'w') as configfile:    config.write(configfile)