Python configparser Module
Example
Read a simple INI string:
import configparser
ini = """[user]\nname = Linus\nport = 8080\n"""
cfg = configparser.ConfigParser()
cfg.read_string(ini)
print(cfg.get("user", "name"))
print(cfg.getint("user", "port"))
Try it Yourself »
Definition and Usage
Use it to read and write configuration files, handle sections and options, and provide default values.
Members
| Member | Description |
|---|---|
| add_section() | Add a new section. |
| ConfigParser() | Main configuration parser class. |
| defaults() | Return the default values for the parser. |
| ExtendedInterpolation | Interpolation strategy supporting ${section:option} syntax. |
| get() | Return an option value as a string. |
| getboolean() | Return a boolean interpreted from an option value. |
| getfloat() | Return a float interpreted from an option value. |
| getint() | Return an int interpreted from an option value. |
| has_option() | Check whether a given option exists in a section. |
| has_section() | Check whether the parser has the named section. |
| Interpolation | Base class for interpolation strategies. |
| items() | Return a list of (name, value) pairs for a section. |
| NoOptionError | Raised when a requested option is not found. |
| NoSectionError | Raised when a requested section is not found. |
| options() | Return a list of option names for a section. |
| read() | Read and parse a filename or a list of filenames. |
| read_dict() | Read configuration from a dictionary. |
| read_file() | Read configuration from an open file object. |
| read_string() | Read configuration from a string. |
| remove_section() | Remove the specified section. |
| SafeConfigParser() | Backwards-compat alias for ConfigParser (deprecated). |
| SectionProxy | Mapping-like object representing a single section. |
| sections() | Return a list of section names. |
| set() | Set an option. |
| write() | Write configuration to a file. |