PyDocs

Nav

File Formats

ConfigParser.read

  • ConfigParser.read(filenames, encoding=None)
Read and parse a filename or an iterable of filenames.

Files that cannot be opened are silently ignored; this is
designed so that you can specify an iterable of potential
configuration file locations (e.g. current directory, user's
home directory, systemwide directory), and all existing
configuration files in the iterable will be read.  A single
filename may also be given.

Return list of successfully read files.
Changed in 3.2, 3.6.1, 3.7
  • 3.2: Added the encoding parameter. Previously, all files were read using the default encoding for open().
  • 3.6.1: The filenames parameter accepts a path-like object.
  • 3.7: The filenames parameter accepts a bytes object.
1 Example
>>> config = configparser.ConfigParser()
>>> config.sections()
[]
>>> config.read('example.ini')
['example.ini']
>>> config.sections()
['forge.example', 'topsecret.server.example']
>>> 'forge.example' in config
True
>>> 'python.org' in config
False
>>> config['forge.example']['User']
'hg'
>>> config['DEFAULT']['Compression']
'yes'
>>> topsecret = config['topsecret.server.example']
>>> topsecret['ForwardX11']
'no'
>>> topsecret['Port']
'50022'
>>> for key in config['forge.example']:
...     print(key)
user
compressionlevel
serveraliveinterval
compression
forwardx11
>>> config['forge.example']['ForwardX11']
'yes'
from the Python docs
See Also
No see-alsos for configparser/ConfigParser.read
0 Notes
No notes for ConfigParser.read