Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change value in ini file using ConfigParser Python

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.

like image 390
CarefullyAdvanced Avatar asked Jan 15 '15 13:01

CarefullyAdvanced


People also ask

How do I update my config INI file?

Open the Backup Manager installation folder. Find the config. ini file inside (see the table below for the exact file path on each operating system) and open as an Administrator. Edit the settings as needed, save the changes and close the file.

How do I use Configparser in Python?

Read and parse one configuration file, given as a file object. Read configuration from a given string. Read configuration from a dictionary. Keys are section names, values are dictionaries with keys and values that should be present in the section.

What is config () in Python?

A Python configuration file is a pure Python file that populates a configuration object. This configuration object is a Config instance.

What is configparser in Python?

Python Programming Server Side Programming The configparser module from Python's standard library defines functionality for reading and writing configuration files as used by Microsoft Windows OS. Such files usually have.INI extension. The INI file consists of sections, each led by a [section] header.

How do I write to a configuration file using configparser?

An example of writing to a configuration file: import configparser config = configparser.RawConfigParser() # Please note that using RawConfigParser's set functions, you can assign # non-string values to keys internally, but will receive an error when # attempting to write to a file or when you get it in non-raw mode.

How do config parsers handle integer values?

Config parsers do not guess datatypes of values in configuration files, always storing them internally as strings. This means that if you need other datatypes, you should convert on your own: Since this task is so common, config parsers provide a range of handy getter methods to handle integers, floats and booleans.

How to implement mapping interface in configparser?

In case of configparser , the mapping interface implementation is using the parser ['section'] ['option'] notation. parser ['section'] in particular returns a proxy for the section’s data in the parser. This means that the values are not copied but they are taken from the original parser on demand.


2 Answers

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)
like image 172
Zah Avatar answered Oct 12 '22 17:10

Zah


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)
like image 42
Vishal Tyagi Avatar answered Oct 12 '22 18:10

Vishal Tyagi