I have a config.ini file and I am trying to delete a specific value in a section of my config.ini file. I basically want to delete an index within a list. The config.ini file is:
[INPUT]
intervals = [[4000, 6000], [25000, 55000]]
For example, I want to delete [4000, 6000], so that the new config.ini file becomes:
[INPUT]
intervals = [[25000, 55000]]
My code is below with two functions, one to import the contents of config.ini (load_args()) and another to delete a section of config.ini (delete_args()). The delete function is based off an answer to How to remove a section from an ini file using Python ConfigParser?
import configparser
import ast
def load_args():
config = configparser.ConfigParser()
config.read('config.ini')
interval = ast.literal_eval(config['INPUT']['intervals'])
print(interval)
load_args()
def delete_args():
config = configparser.ConfigParser()
with open('config.ini', 'r+') as s:
config.readfp(s)
config.remove_section('INPUT')
s.seek(0)
config.write(s)
s.truncate()
interval = ast.literal_eval(config['INPUT']['intervals'])
print(interval)
delete_args()
The current delete_args() function above deletes everything in the config.ini. I understand that delete_args() needs a way to find the value [4000, 6000] in config.ini, so I was wondering if is possible to pass in [4000, 6000] as an argument into delete_args() that then finds the section [INPUT], and finds the specified interval ([4000, 6000]) within intervals and deletes it.