Change value in ini file using ConfigParser Python

49,065

Solution 1

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)

Solution 2

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

import configparser

config = configparser.ConfigParser()
config.read('settings.ini')
config.set('SETTINGS', 'value','15')

with open('settings.ini', 'w') as configfile:
    config.write(configfile)

Solution 3

I had an issue with:with open

Other way:

import configparser

def 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

Solution 4

Below example will help change the value in the ini file:

PROJECT_HOME="/test/"
parser = ConfigParser()
parser.read("{}/conf/cdc_config.ini".format(PROJECT_HOME))
parser.set("default","project_home",str(PROJECT_HOME))


with open('{}/conf/cdc_config.ini'.format(PROJECT_HOME), 'w') as configfile:
    parser.write(configfile)
[default]
project_home = /Mypath/
Share:
49,065
CarefullyAdvanced
Author by

CarefullyAdvanced

Updated on July 09, 2022

Comments

  • CarefullyAdvanced
    CarefullyAdvanced almost 2 years

    So, I have this settings.ini :

    [SETTINGS]
    
    value = 1
    

    And this python script

    from ConfigParser import SafeConfigParser
    
    parser = SafeConfigParser()
    parser.read('settings.ini')
    
    print parser.get('SETTINGS', 'value')
    

    As you can see, I want to read and then replace the value "1" by another one. All I was able to do so far is to read it. I searched on the net how to replace it but I didn't find.

  • ishahak
    ishahak about 5 years
    For Python 3.5+ you have to use 'w', not 'wb'!
  • skpro19
    skpro19 almost 4 years
    deleted my original config.ini file
  • Jan
    Jan almost 4 years
    @skpro19 I was trying again on linux and win. ini and properties file behave the same. No issue with deleted file. Win (python 3.4.1) linux (python 3.6.8). I was trying with various configuration of file/directory access
  • AlperenK
    AlperenK over 3 years
    SafeConfigParser deprecated ``` DeprecationWarning: The SafeConfigParser class has been renamed to ConfigParser in Python 3.2. This alias will be removed in future versions. Use ConfigParser directly instead. 'os':operating_system, 'arch':arch}) ```
  • hc_dev
    hc_dev almost 3 years
    If you make your code a complete example, it would show declaration of parser and the filename would match given question, i.e. 'settings.ini' to actually change the file contents.
  • hc_dev
    hc_dev almost 3 years
    Don't need to justify why not using with open ... Your solution works. Only the comment has a small typo ("case case") and Java-related properties file note is a bit confusing here, because question is about INI file 😏️
  • deokyong song
    deokyong song about 2 years
    Question, in your snippet, these curly brackets in open method, {} they mean relative path?
  • Lachlan
    Lachlan about 2 years
    @deokyong song that is a formatting method. see this link for more information on string formatting.