Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python ConfigParser module rename a section

How can you rename a section in a ConfigParser object?

like image 773
bph Avatar asked Jul 23 '26 01:07

bph


2 Answers

example helper function - silly really but it might save someone a few minutes work...

def rename_section(cp, section_from, section_to):
    items = cp.items(section_from)
    cp.add_section(section_to)
    for item in items:
        cp.set(section_to, item[0], item[1])
    cp.remove_section(section_from)
like image 134
bph Avatar answered Jul 25 '26 15:07

bph


As far as I can tell, you need to

  • get the sections items via ConfigParser.items
  • remove the section via ConfigParser.remove_section
  • create a new section via ConfigParser.add_section
  • Put the items back into the new section via ConfigParser.set
like image 35
mgilson Avatar answered Jul 25 '26 13:07

mgilson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!