Change value in ini file using ConfigParser Python
Asked Answered
E

4

25

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.

Exarate answered 15/1, 2015 at 13:4 Comment(0)
W
40

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)
Whoop answered 15/1, 2015 at 13:12 Comment(3)
For Python 3.5+ you have to use 'w', not 'wb'!Wavemeter
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}) ```Dunne
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.Bjork
C
11

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)
Compliancy answered 27/7, 2021 at 15:34 Comment(0)
P
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

Pals answered 8/6, 2020 at 6:50 Comment(3)
deleted my original config.ini fileVicentevicepresident
@Vicentevicepresident 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 accessPals
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 😏️Bjork
C
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/
Cutlor answered 20/5, 2020 at 20:1 Comment(3)
Question, in your snippet, these curly brackets in open method, {} they mean relative path?Amero
@deokyong song that is a formatting method. see this link for more information on string formatting.Hospitaler
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)Observer

© 2022 - 2024 — McMap. All rights reserved.