26

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.

4 Answers 4

42

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)
Sign up to request clarification or add additional context in comments.

3 Comments

For Python 3.5+ you have to use 'w', not 'wb'!
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}) ```
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.
12

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)

Comments

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

3 Comments

deleted my original config.ini file
@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
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 😏️
2

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/

3 Comments

Question, in your snippet, these curly brackets in open method, {} they mean relative path?
@deokyong song that is a formatting method. see this link for more information on string formatting.
I think that str(PROJECT_HOME) is not necessary, because PROJECT_HOME is defined as a string. So, I think, this should work the same: parser.set("default","project_home",PROJECT_HOME)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.