4. Management of sections¶
4.1. Checking if has a section¶
For checking existing section you can use method has_section()
:
if not config.has_section('section1'):
print('Not exists')
or use iteration:
if not 'section1' in config:
print('Not exists')
4.2. Add new section¶
Adding a new section or reset exist section:
new_section = config.add_section('section1')
Also, you can add new section with a leading spaces or add comment after section name:
new_section = config.add_section('section1', text_before=' ', text_after=' # This is comment')
4.3. Get an section¶
Get non-section area for management of simple configs:
non_section = config[None]
non_section = config.get_section()
Also you can get an existing section in two different ways:
section1 = config['section1']
section1 = config.get_section('section1')
Note
if section do not exist then raised KeyError