开发者

Rename config.ini section using ConfigParser in python

开发者 https://www.devze.com 2023-01-11 04:20 出处:网络
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开发者_Python百科 and recreate it, but that is my only answer right

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开发者_Python百科 and recreate it, but that is my only answer right now.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消