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.
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.
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.
A Python configuration file is a pure Python file that populates a configuration object. This configuration object is a Config instance.
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.
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.
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.
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.
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With