Is there an easy way to rename a section in a config file using ConfigParser in python? I’d prefer not to have to delete the section and recreate it, but that is my only answer right now.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No. The builtin ConfigParser stores sections as
_sections, which is a dict. Since this is python you could access that variable to do an easy copy.(
config._sections[new_name] = config._sections[old_name]; config._sections.pop(old_name)But ConfigParser may change at some later date and this would break your implementation.
Therefore I don’t recommend doing that, alternatively you could subclass ConfigParser and implement a delete_section or change the way options are stored.