Rename config.ini section using ConfigParser in python

心不动则不痛 提交于 2019-12-12 01:32:25

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/3452788/rename-config-ini-section-using-configparser-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!