23

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.

MANOJ GOPI
  • 1,279
  • 10
  • 31
CarefullyAdvanced
  • 437
  • 1
  • 4
  • 15

4 Answers4

39

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)
Zah
  • 6,394
  • 1
  • 22
  • 34
  • 18
    For Python 3.5+ you have to use 'w', not 'wb'! – ishahak Mar 07 '19 at 15:22
  • 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}) ``` – AlperenK Sep 07 '20 at 10:48
  • 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 Jul 27 '21 at 21:08
7

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)
hc_dev
  • 8,389
  • 1
  • 26
  • 38
Vishal Tyagi
  • 141
  • 1
  • 4
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

Jan
  • 111
  • 1
  • 5
  • deleted my original config.ini file – skpro19 Jul 18 '20 at 10:32
  • @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 – Jan Jul 20 '20 at 12:12
  • 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](https://en.wikipedia.org/wiki/.properties) note is a bit confusing here, because question is about [INI file](https://en.wikipedia.org/wiki/INI_file) ️ – hc_dev Jul 27 '21 at 21:15
1

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/
Sampat Kumar
  • 492
  • 1
  • 6
  • 14
  • Question, in your snippet, these curly brackets in open method, {} they mean relative path? – deokyong song Mar 18 '22 at 03:42
  • 1
    @deokyong song that is a formatting method. see [this link](https://docs.python.org/3/library/stdtypes.html#str.format) for more information on string formatting. – Lachlan Mar 25 '22 at 04:49