2
How to save the configuration file (.ini
) after deleting an option or section?
I’m doing it this way:
test ini.
[LOCAL]
url = 'http://localhost:8080/'
username = 'usuario'
password = 'SECRET'
[TESTE]
url = 'http://localhost:8080/teste/'
username = 'usuario'
password = 'SECRET'
removes.py
import configparser
parser = configparser.ConfigParser()
parser.read('teste.ini')
print('Lendo Valores:')
for section in parser.sections():
print(section)
for name, value in parser.items(section):
print(f"{name} = {value}")
parser.remove_option('LOCAL', 'password')
parser.remove_section('TESTE')
print('\Valor Modificado:')
for section in parser.sections():
print(section)
for name, value in parser.items(section):
print(f"{name} = {value}")
However I can’t save the change, in the tests I did using some examples I found on the internet the file duplicates some TAGS, saves only half the file and sometimes even saves.
Just one detail, you don’t need to run
ArqTeste.close()
for thewith
already does it for you.– fernandosavio