Skip to content

Commit fc21781

Browse files
timonviolaambv
andauthored
gh-96765: Update ConfigParser.read() docs with multi-file read example (#121664)
Co-authored-by: Łukasz Langa <[email protected]>
1 parent 4b9e10d commit fc21781

File tree

1 file changed

+44
-14
lines changed

1 file changed

+44
-14
lines changed

Doc/library/configparser.rst

+44-14
Original file line numberDiff line numberDiff line change
@@ -147,23 +147,28 @@ case-insensitive and stored in lowercase [1]_.
147147
It is possible to read several configurations into a single
148148
:class:`ConfigParser`, where the most recently added configuration has the
149149
highest priority. Any conflicting keys are taken from the more recent
150-
configuration while the previously existing keys are retained.
150+
configuration while the previously existing keys are retained. The example
151+
below reads in an ``override.ini`` file, which will override any conflicting
152+
keys from the ``example.ini`` file.
153+
154+
.. code-block:: ini
155+
156+
[DEFAULT]
157+
ServerAliveInterval = -1
151158
152159
.. doctest::
153160

154-
>>> another_config = configparser.ConfigParser()
155-
>>> another_config.read('example.ini')
156-
['example.ini']
157-
>>> another_config['topsecret.server.example']['Port']
158-
'50022'
159-
>>> another_config.read_string("[topsecret.server.example]\nPort=48484")
160-
>>> another_config['topsecret.server.example']['Port']
161-
'48484'
162-
>>> another_config.read_dict({"topsecret.server.example": {"Port": 21212}})
163-
>>> another_config['topsecret.server.example']['Port']
164-
'21212'
165-
>>> another_config['topsecret.server.example']['ForwardX11']
166-
'no'
161+
>>> config_override = configparser.ConfigParser()
162+
>>> config_override['DEFAULT'] = {'ServerAliveInterval': '-1'}
163+
>>> with open('override.ini', 'w') as configfile:
164+
... config_override.write(configfile)
165+
...
166+
>>> config_override = configparser.ConfigParser()
167+
>>> config_override.read(['example.ini', 'override.ini'])
168+
['example.ini', 'override.ini']
169+
>>> print(config_override.get('DEFAULT', 'ServerAliveInterval'))
170+
-1
171+
167172

168173
This behaviour is equivalent to a :meth:`ConfigParser.read` call with several
169174
files passed to the *filenames* parameter.
@@ -984,6 +989,31 @@ ConfigParser Objects
984989
converter gets its own corresponding :meth:`!get*()` method on the parser
985990
object and section proxies.
986991

992+
It is possible to read several configurations into a single
993+
:class:`ConfigParser`, where the most recently added configuration has the
994+
highest priority. Any conflicting keys are taken from the more recent
995+
configuration while the previously existing keys are retained. The example
996+
below reads in an ``override.ini`` file, which will override any conflicting
997+
keys from the ``example.ini`` file.
998+
999+
.. code-block:: ini
1000+
1001+
[DEFAULT]
1002+
ServerAliveInterval = -1
1003+
1004+
.. doctest::
1005+
1006+
>>> config_override = configparser.ConfigParser()
1007+
>>> config_override['DEFAULT'] = {'ServerAliveInterval': '-1'}
1008+
>>> with open('override.ini', 'w') as configfile:
1009+
... config_override.write(configfile)
1010+
...
1011+
>>> config_override = configparser.ConfigParser()
1012+
>>> config_override.read(['example.ini', 'override.ini'])
1013+
['example.ini', 'override.ini']
1014+
>>> print(config_override.get('DEFAULT', 'ServerAliveInterval'))
1015+
-1
1016+
9871017
.. versionchanged:: 3.1
9881018
The default *dict_type* is :class:`collections.OrderedDict`.
9891019

0 commit comments

Comments
 (0)