/usr/share/doc/python2-docs/html/_sources/library
NameSizeModeActions
2to3.rst.txt146470644editdlrm
abc.rst.txt72190644editdlrm
aepack.rst.txt42570644editdlrm
aetools.rst.txt35320644editdlrm
aetypes.rst.txt42570644editdlrm
aifc.rst.txt70860644editdlrm
al.rst.txt53090644editdlrm
allos.rst.txt6950644editdlrm
anydbm.rst.txt41110644editdlrm
archiving.rst.txt4240644editdlrm
argparse.rst.txt746850644editdlrm
array.rst.txt105240644editdlrm
ast.rst.txt101110644editdlrm
asynchat.rst.txt92060644editdlrm
asyncore.rst.txt129370644editdlrm
atexit.rst.txt39100644editdlrm
audioop.rst.txt103950644editdlrm
autogil.rst.txt10150644editdlrm
base64.rst.txt62520644editdlrm
basehttpserver.rst.txt104040644editdlrm
bastion.rst.txt26110644editdlrm
bdb.rst.txt124560644editdlrm
binascii.rst.txt65080644editdlrm
binhex.rst.txt19100644editdlrm
bisect.rst.txt54150644editdlrm
bsddb.rst.txt75750644editdlrm
bz2.rst.txt80640644editdlrm
calendar.rst.txt112820644editdlrm
carbon.rst.txt159560644editdlrm
cd.rst.txt119740644editdlrm
cgi.rst.txt228530644editdlrm
cgihttpserver.rst.txt27880644editdlrm
cgitb.rst.txt28740644editdlrm
chunk.rst.txt49550644editdlrm
cmath.rst.txt76470644editdlrm
cmd.rst.txt85650644editdlrm
code.rst.txt71150644editdlrm
codecs.rst.txt669660644editdlrm
codeop.rst.txt37740644editdlrm
collections.rst.txt414480644editdlrm
colorpicker.rst.txt9130644editdlrm
colorsys.rst.txt18190644editdlrm
commands.rst.txt25950644editdlrm
compileall.rst.txt46780644editdlrm
compiler.rst.txt374640644editdlrm
configparser.rst.txt196070644editdlrm
constants.rst.txt23280644editdlrm
contextlib.rst.txt60120644editdlrm
cookie.rst.txt95430644editdlrm
cookielib.rst.txt278750644editdlrm
copy.rst.txt33510644editdlrm
copy_reg.rst.txt23290644editdlrm
crypt.rst.txt22920644editdlrm
crypto.rst.txt3550644editdlrm
csv.rst.txt227570644editdlrm
ctypes.rst.txt905130644editdlrm
curses.ascii.rst.txt90460644editdlrm
curses.panel.rst.txt27400644editdlrm
curses.rst.txt748770644editdlrm
custominterp.rst.txt5700644editdlrm
datatypes.rst.txt8640644editdlrm
datetime.rst.txt746790644editdlrm
dbhash.rst.txt38650644editdlrm
dbm.rst.txt31170644editdlrm
debug.rst.txt4460644editdlrm
decimal.rst.txt709260644editdlrm
development.rst.txt6400644editdlrm
difflib.rst.txt307160644editdlrm
dircache.rst.txt18130644editdlrm
dis.rst.txt232480644editdlrm
distribution.rst.txt4260644editdlrm
distutils.rst.txt19580644editdlrm
dl.rst.txt33920644editdlrm
doctest.rst.txt739850644editdlrm
docxmlrpcserver.rst.txt38020644editdlrm
dumbdbm.rst.txt28410644editdlrm
dummy_thread.rst.txt10580644editdlrm
dummy_threading.rst.txt7990644editdlrm
easydialogs.rst.txt103460644editdlrm
email-examples.rst.txt12710644editdlrm
email.charset.rst.txt96540644editdlrm
email.encoders.rst.txt23760644editdlrm
email.errors.rst.txt40060644editdlrm
email.generator.rst.txt61320644editdlrm
email.header.rst.txt75310644editdlrm
email.iterators.rst.txt24210644editdlrm
email.message.rst.txt252170644editdlrm
email.mime.rst.txt99170644editdlrm
email.parser.rst.txt103270644editdlrm
email.rst.txt161110644editdlrm
email.utils.rst.txt64750644editdlrm
ensurepip.rst.txt50220644editdlrm
errno.rst.txt67080644editdlrm
exceptions.rst.txt183430644editdlrm
fcntl.rst.txt73610644editdlrm
filecmp.rst.txt53480644editdlrm
fileformats.rst.txt3020644editdlrm
fileinput.rst.txt74170644editdlrm
filesys.rst.txt8060644editdlrm
fl.rst.txt176460644editdlrm
fm.rst.txt26990644editdlrm
fnmatch.rst.txt31040644editdlrm
formatter.rst.txt132450644editdlrm
fpectl.rst.txt41710644editdlrm
fpformat.rst.txt17470644editdlrm
fractions.rst.txt52970644editdlrm
framework.rst.txt114440644editdlrm
frameworks.rst.txt3780644editdlrm
ftplib.rst.txt157200644editdlrm
functions.rst.txt755540644editdlrm
functools.rst.txt74480644editdlrm
future_builtins.rst.txt20100644editdlrm
gc.rst.txt90140644editdlrm
gdbm.rst.txt48820644editdlrm
gensuitemodule.rst.txt31130644editdlrm
getopt.rst.txt66700644editdlrm
getpass.rst.txt18760644editdlrm
gettext.rst.txt290400644editdlrm
gl.rst.txt60090644editdlrm
glob.rst.txt24140644editdlrm
grp.rst.txt22560644editdlrm
gzip.rst.txt48280644editdlrm
hashlib.rst.txt73790644editdlrm
heapq.rst.txt131850644editdlrm
hmac.rst.txt30730644editdlrm
hotshot.rst.txt42890644editdlrm
htmllib.rst.txt73790644editdlrm
htmlparser.rst.txt116400644editdlrm
httplib.rst.txt374540644editdlrm
i18n.rst.txt4090644editdlrm
ic.rst.txt50050644editdlrm
idle.rst.txt221820644editdlrm
imageop.rst.txt40010644editdlrm
imaplib.rst.txt172210644editdlrm
imgfile.rst.txt27650644editdlrm
imghdr.rst.txt26350644editdlrm
imp.rst.txt125930644editdlrm
importlib.rst.txt11260644editdlrm
imputil.rst.txt70230644editdlrm
index.rst.txt22870644editdlrm
inspect.rst.txt281510644editdlrm
internet.rst.txt9500644editdlrm
intro.rst.txt28030644editdlrm
io.rst.txt390150644editdlrm
ipc.rst.txt6300644editdlrm
itertools.rst.txt363240644editdlrm
jpeg.rst.txt38580644editdlrm
json.rst.txt255450644editdlrm
keyword.rst.txt6170644editdlrm
language.rst.txt5230644editdlrm
linecache.rst.txt18870644editdlrm
locale.rst.txt249750644editdlrm
logging.config.rst.txt315600644editdlrm
logging.handlers.rst.txt281530644editdlrm
logging.rst.txt468820644editdlrm
mac.rst.txt7910644editdlrm
macos.rst.txt38240644editdlrm
macosa.rst.txt39640644editdlrm
macostools.rst.txt40170644editdlrm
macpath.rst.txt6500644editdlrm
mailbox.rst.txt681290644editdlrm
mailcap.rst.txt36750644editdlrm
markup.rst.txt12490644editdlrm
marshal.rst.txt56890644editdlrm
math.rst.txt109200644editdlrm
md5.rst.txt28150644editdlrm
mhlib.rst.txt39660644editdlrm
mimetools.rst.txt45040644editdlrm
mimetypes.rst.txt98390644editdlrm
mimewriter.rst.txt33600644editdlrm
mimify.rst.txt35190644editdlrm
miniaeframe.rst.txt25640644editdlrm
misc.rst.txt2480644editdlrm
mm.rst.txt4470644editdlrm
mmap.rst.txt104810644editdlrm
modulefinder.rst.txt33800644editdlrm
modules.rst.txt3820644editdlrm
msilib.rst.txt190620644editdlrm
msvcrt.rst.txt43460644editdlrm
multifile.rst.txt66130644editdlrm
multiprocessing.rst.txt926210644editdlrm
mutex.rst.txt19390644editdlrm
netdata.rst.txt4320644editdlrm
netrc.rst.txt31190644editdlrm
new.rst.txt26530644editdlrm
nis.rst.txt21080644editdlrm
nntplib.rst.txt145310644editdlrm
numbers.rst.txt80070644editdlrm
numeric.rst.txt7510644editdlrm
operator.rst.txt220920644editdlrm
optparse.rst.txt771010644editdlrm
os.path.rst.txt130940644editdlrm
os.rst.txt825800644editdlrm
ossaudiodev.rst.txt173090644editdlrm
othergui.rst.txt27210644editdlrm
parser.rst.txt153850644editdlrm
pdb.rst.txt160430644editdlrm
persistence.rst.txt8260644editdlrm
pickle.rst.txt372620644editdlrm
pickletools.rst.txt19970644editdlrm
pipes.rst.txt37860644editdlrm
pkgutil.rst.txt77140644editdlrm
platform.rst.txt95540644editdlrm
plistlib.rst.txt41340644editdlrm
popen2.rst.txt70220644editdlrm
poplib.rst.txt62200644editdlrm
posix.rst.txt36300644editdlrm
posixfile.rst.txt72000644editdlrm
pprint.rst.txt90710644editdlrm
profile.rst.txt286660644editdlrm
pty.rst.txt17620644editdlrm
pwd.rst.txt27250644editdlrm
pyclbr.rst.txt32960644editdlrm
pydoc.rst.txt40830644editdlrm
pyexpat.rst.txt288910644editdlrm
python.rst.txt5140644editdlrm
py_compile.rst.txt24790644editdlrm
queue.rst.txt70340644editdlrm
quopri.rst.txt26700644editdlrm
random.rst.txt133190644editdlrm
re.rst.txt551020644editdlrm
readline.rst.txt105130644editdlrm
repr.rst.txt47180644editdlrm
resource.rst.txt95950644editdlrm
restricted.rst.txt33270644editdlrm
rexec.rst.txt117420644editdlrm
rfc822.rst.txt140370644editdlrm
rlcompleter.rst.txt24940644editdlrm
robotparser.rst.txt21900644editdlrm
runpy.rst.txt69380644editdlrm
sched.rst.txt46440644editdlrm
scrolledtext.rst.txt13790644editdlrm
select.rst.txt207050644editdlrm
sets.rst.txt149730644editdlrm
sgi.rst.txt3220644editdlrm
sgmllib.rst.txt106640644editdlrm
sha.rst.txt28070644editdlrm
shelve.rst.txt83190644editdlrm
shlex.rst.txt113130644editdlrm
shutil.rst.txt134910644editdlrm
signal.rst.txt107190644editdlrm
simplehttpserver.rst.txt45590644editdlrm
simplexmlrpcserver.rst.txt108810644editdlrm
site.rst.txt78800644editdlrm
smtpd.rst.txt24640644editdlrm
smtplib.rst.txt149060644editdlrm
sndhdr.rst.txt17590644editdlrm
socket.rst.txt407650644editdlrm
socketserver.rst.txt225120644editdlrm
someos.rst.txt5990644editdlrm
spwd.rst.txt28250644editdlrm
sqlite3.rst.txt354480644editdlrm
ssl.rst.txt765540644editdlrm
stat.rst.txt77780644editdlrm
statvfs.rst.txt13000644editdlrm
stdtypes.rst.txt1226680644editdlrm
string.rst.txt440850644editdlrm
stringio.rst.txt41660644editdlrm
stringprep.rst.txt42380644editdlrm
strings.rst.txt7460644editdlrm
struct.rst.txt171000644editdlrm
subprocess.rst.txt336480644editdlrm
sun.rst.txt2490644editdlrm
sunau.rst.txt71240644editdlrm
sunaudio.rst.txt58490644editdlrm
symbol.rst.txt9750644editdlrm
symtable.rst.txt50640644editdlrm
sys.rst.txt475200644editdlrm
sysconfig.rst.txt76390644editdlrm
syslog.rst.txt39310644editdlrm
tabnanny.rst.txt19990644editdlrm
tarfile.rst.txt282140644editdlrm
telnetlib.rst.txt74820644editdlrm
tempfile.rst.txt105040644editdlrm
termios.rst.txt37330644editdlrm
test.rst.txt176590644editdlrm
textwrap.rst.txt85520644editdlrm
thread.rst.txt65880644editdlrm
threading.rst.txt324680644editdlrm
time.rst.txt260200644editdlrm
timeit.rst.txt115980644editdlrm
tix.rst.txt226930644editdlrm
tk.rst.txt16120644editdlrm
tkinter.rst.txt334560644editdlrm
token.rst.txt24510644editdlrm
tokenize.rst.txt55700644editdlrm
trace.rst.txt67240644editdlrm
traceback.rst.txt107110644editdlrm
ttk.rst.txt574110644editdlrm
tty.rst.txt10110644editdlrm
turtle.rst.txt640970644editdlrm
types.rst.txt62000644editdlrm
undoc.rst.txt65500644editdlrm
unicodedata.rst.txt57360644editdlrm
unittest.rst.txt829520644editdlrm
unix.rst.txt4900644editdlrm
urllib.rst.txt249600644editdlrm
urllib2.rst.txt352110644editdlrm
urlparse.rst.txt160710644editdlrm
user.rst.txt27480644editdlrm
userdict.rst.txt92900644editdlrm
uu.rst.txt23690644editdlrm
uuid.rst.txt83780644editdlrm
warnings.rst.txt198150644editdlrm
wave.rst.txt50470644editdlrm
weakref.rst.txt129260644editdlrm
webbrowser.rst.txt98630644editdlrm
whichdb.rst.txt9310644editdlrm
windows.rst.txt2730644editdlrm
winsound.rst.txt50660644editdlrm
wsgiref.rst.txt305670644editdlrm
xdrlib.rst.txt80770644editdlrm
xml.dom.minidom.rst.txt112080644editdlrm
xml.dom.pulldom.rst.txt15710644editdlrm
xml.dom.rst.txt401820644editdlrm
xml.etree.elementtree.rst.txt357070644editdlrm
xml.rst.txt60900644editdlrm
xml.sax.handler.rst.txt153710644editdlrm
xml.sax.reader.rst.txt122570644editdlrm
xml.sax.rst.txt64600644editdlrm
xml.sax.utils.rst.txt35600644editdlrm
xmlrpclib.rst.txt225590644editdlrm
zipfile.rst.txt188710644editdlrm
zipimport.rst.txt59570644editdlrm
zlib.rst.txt127660644editdlrm
_winreg.rst.txt233050644editdlrm
__builtin__.rst.txt14940644editdlrm
__future__.rst.txt49520644editdlrm
__main__.rst.txt5350644editdlrm
Edit: /usr/share/doc/python2-docs/html/_sources/library/configparser.rst.txt (19607B)
:mod:`ConfigParser` --- Configuration file parser ================================================= .. module:: ConfigParser :synopsis: Configuration file parser. .. moduleauthor:: Ken Manheimer .. moduleauthor:: Barry Warsaw .. moduleauthor:: Eric S. Raymond .. sectionauthor:: Christopher G. Petrilli .. note:: The :mod:`ConfigParser` module has been renamed to :mod:`configparser` in Python 3. The :term:`2to3` tool will automatically adapt imports when converting your sources to Python 3. .. index:: pair: .ini; file pair: configuration; file single: ini file single: Windows ini file This module defines the class :class:`~ConfigParser.ConfigParser`. The :class:`~ConfigParser.ConfigParser` class implements a basic configuration file parser language which provides a structure similar to what you would find on Microsoft Windows INI files. You can use this to write Python programs which can be customized by end users easily. .. note:: This library does *not* interpret or write the value-type prefixes used in the Windows Registry extended version of INI syntax. .. seealso:: Module :mod:`shlex` Support for creating Unix shell-like mini-languages which can be used as an alternate format for application configuration files. Module :mod:`json` The json module implements a subset of JavaScript syntax which can also be used for this purpose. The configuration file consists of sections, led by a ``[section]`` header and followed by ``name: value`` entries, with continuations in the style of :rfc:`822` (see section 3.1.1, "LONG HEADER FIELDS"); ``name=value`` is also accepted. Note that leading whitespace is removed from values. The optional values can contain format strings which refer to other values in the same section, or values in a special ``DEFAULT`` section. Additional defaults can be provided on initialization and retrieval. Lines beginning with ``'#'`` or ``';'`` are ignored and may be used to provide comments. Configuration files may include comments, prefixed by specific characters (``#`` and ``;``). Comments may appear on their own in an otherwise empty line, or may be entered in lines holding values or section names. In the latter case, they need to be preceded by a whitespace character to be recognized as a comment. (For backwards compatibility, only ``;`` starts an inline comment, while ``#`` does not.) On top of the core functionality, :class:`SafeConfigParser` supports interpolation. This means values can contain format strings which refer to other values in the same section, or values in a special ``DEFAULT`` section. Additional defaults can be provided on initialization. For example:: [My Section] foodir: %(dir)s/whatever dir=frob long: this value continues in the next line would resolve the ``%(dir)s`` to the value of ``dir`` (``frob`` in this case). All reference expansions are done on demand. Default values can be specified by passing them into the :class:`~ConfigParser.ConfigParser` constructor as a dictionary. Additional defaults may be passed into the :meth:`get` method which will override all others. Sections are normally stored in a built-in dictionary. An alternative dictionary type can be passed to the :class:`~ConfigParser.ConfigParser` constructor. For example, if a dictionary type is passed that sorts its keys, the sections will be sorted on write-back, as will be the keys within each section. .. class:: RawConfigParser([defaults[, dict_type[, allow_no_value]]]) The basic configuration object. When *defaults* is given, it is initialized into the dictionary of intrinsic defaults. When *dict_type* is given, it will be used to create the dictionary objects for the list of sections, for the options within a section, and for the default values. When *allow_no_value* is true (default: ``False``), options without values are accepted; the value presented for these is ``None``. This class does not support the magical interpolation behavior. All option names are passed through the :meth:`optionxform` method. Its default implementation converts option names to lower case. .. versionadded:: 2.3 .. versionchanged:: 2.6 *dict_type* was added. .. versionchanged:: 2.7 The default *dict_type* is :class:`collections.OrderedDict`. *allow_no_value* was added. .. class:: ConfigParser([defaults[, dict_type[, allow_no_value]]]) Derived class of :class:`RawConfigParser` that implements the magical interpolation feature and adds optional arguments to the :meth:`get` and :meth:`items` methods. The values in *defaults* must be appropriate for the ``%()s`` string interpolation. Note that *__name__* is an intrinsic default; its value is the section name, and will override any value provided in *defaults*. All option names used in interpolation will be passed through the :meth:`optionxform` method just like any other option name reference. Using the default implementation of :meth:`optionxform`, the values ``foo %(bar)s`` and ``foo %(BAR)s`` are equivalent. .. versionadded:: 2.3 .. versionchanged:: 2.6 *dict_type* was added. .. versionchanged:: 2.7 The default *dict_type* is :class:`collections.OrderedDict`. *allow_no_value* was added. .. class:: SafeConfigParser([defaults[, dict_type[, allow_no_value]]]) Derived class of :class:`~ConfigParser.ConfigParser` that implements a more-sane variant of the magical interpolation feature. This implementation is more predictable as well. New applications should prefer this version if they don't need to be compatible with older versions of Python. .. XXX Need to explain what's safer/more predictable about it. .. versionadded:: 2.3 .. versionchanged:: 2.6 *dict_type* was added. .. versionchanged:: 2.7 The default *dict_type* is :class:`collections.OrderedDict`. *allow_no_value* was added. .. exception:: Error Base class for all other configparser exceptions. .. exception:: NoSectionError Exception raised when a specified section is not found. .. exception:: DuplicateSectionError Exception raised if :meth:`add_section` is called with the name of a section that is already present. .. exception:: NoOptionError Exception raised when a specified option is not found in the specified section. .. exception:: InterpolationError Base class for exceptions raised when problems occur performing string interpolation. .. exception:: InterpolationDepthError Exception raised when string interpolation cannot be completed because the number of iterations exceeds :const:`MAX_INTERPOLATION_DEPTH`. Subclass of :exc:`InterpolationError`. .. exception:: InterpolationMissingOptionError Exception raised when an option referenced from a value does not exist. Subclass of :exc:`InterpolationError`. .. versionadded:: 2.3 .. exception:: InterpolationSyntaxError Exception raised when the source text into which substitutions are made does not conform to the required syntax. Subclass of :exc:`InterpolationError`. .. versionadded:: 2.3 .. exception:: MissingSectionHeaderError Exception raised when attempting to parse a file which has no section headers. .. exception:: ParsingError Exception raised when errors occur attempting to parse a file. .. data:: MAX_INTERPOLATION_DEPTH The maximum depth for recursive interpolation for :meth:`get` when the *raw* parameter is false. This is relevant only for the :class:`~ConfigParser.ConfigParser` class. .. seealso:: Module :mod:`shlex` Support for a creating Unix shell-like mini-languages which can be used as an alternate format for application configuration files. .. _rawconfigparser-objects: RawConfigParser Objects ----------------------- :class:`RawConfigParser` instances have the following methods: .. method:: RawConfigParser.defaults() Return a dictionary containing the instance-wide defaults. .. method:: RawConfigParser.sections() Return a list of the sections available; ``DEFAULT`` is not included in the list. .. method:: RawConfigParser.add_section(section) Add a section named *section* to the instance. If a section by the given name already exists, :exc:`DuplicateSectionError` is raised. If the name ``DEFAULT`` (or any of it's case-insensitive variants) is passed, :exc:`ValueError` is raised. .. method:: RawConfigParser.has_section(section) Indicates whether the named section is present in the configuration. The ``DEFAULT`` section is not acknowledged. .. method:: RawConfigParser.options(section) Returns a list of options available in the specified *section*. .. method:: RawConfigParser.has_option(section, option) If the given section exists, and contains the given option, return :const:`True`; otherwise return :const:`False`. .. versionadded:: 1.6 .. method:: RawConfigParser.read(filenames) Attempt to read and parse a list of filenames, returning a list of filenames which were successfully parsed. If *filenames* is a string or Unicode string, it is treated as a single filename. If a file named in *filenames* cannot be opened, that file will be ignored. This is designed so that you can specify a list of potential configuration file locations (for example, the current directory, the user's home directory, and some system-wide directory), and all existing configuration files in the list will be read. If none of the named files exist, the :class:`~ConfigParser.ConfigParser` instance will contain an empty dataset. An application which requires initial values to be loaded from a file should load the required file or files using :meth:`readfp` before calling :meth:`read` for any optional files:: import ConfigParser, os config = ConfigParser.ConfigParser() config.readfp(open('defaults.cfg')) config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')]) .. versionchanged:: 2.4 Returns list of successfully parsed filenames. .. method:: RawConfigParser.readfp(fp[, filename]) Read and parse configuration data from the file or file-like object in *fp* (only the :meth:`readline` method is used). If *filename* is omitted and *fp* has a :attr:`name` attribute, that is used for *filename*; the default is ````. .. method:: RawConfigParser.get(section, option) Get an *option* value for the named *section*. .. method:: RawConfigParser.getint(section, option) A convenience method which coerces the *option* in the specified *section* to an integer. .. method:: RawConfigParser.getfloat(section, option) A convenience method which coerces the *option* in the specified *section* to a floating point number. .. method:: RawConfigParser.getboolean(section, option) A convenience method which coerces the *option* in the specified *section* to a Boolean value. Note that the accepted values for the option are ``"1"``, ``"yes"``, ``"true"``, and ``"on"``, which cause this method to return ``True``, and ``"0"``, ``"no"``, ``"false"``, and ``"off"``, which cause it to return ``False``. These string values are checked in a case-insensitive manner. Any other value will cause it to raise :exc:`ValueError`. .. method:: RawConfigParser.items(section) Return a list of ``(name, value)`` pairs for each option in the given *section*. .. method:: RawConfigParser.set(section, option, value) If the given section exists, set the given option to the specified value; otherwise raise :exc:`NoSectionError`. While it is possible to use :class:`RawConfigParser` (or :class:`~ConfigParser.ConfigParser` with *raw* parameters set to true) for *internal* storage of non-string values, full functionality (including interpolation and output to files) can only be achieved using string values. .. versionadded:: 1.6 .. method:: RawConfigParser.write(fileobject) Write a representation of the configuration to the specified file object. This representation can be parsed by a future :meth:`read` call. .. versionadded:: 1.6 .. method:: RawConfigParser.remove_option(section, option) Remove the specified *option* from the specified *section*. If the section does not exist, raise :exc:`NoSectionError`. If the option existed to be removed, return :const:`True`; otherwise return :const:`False`. .. versionadded:: 1.6 .. method:: RawConfigParser.remove_section(section) Remove the specified *section* from the configuration. If the section in fact existed, return ``True``. Otherwise return ``False``. .. method:: RawConfigParser.optionxform(option) Transforms the option name *option* as found in an input file or as passed in by client code to the form that should be used in the internal structures. The default implementation returns a lower-case version of *option*; subclasses may override this or client code can set an attribute of this name on instances to affect this behavior. You don't necessarily need to subclass a ConfigParser to use this method, you can also re-set it on an instance, to a function that takes a string argument. Setting it to ``str``, for example, would make option names case sensitive:: cfgparser = ConfigParser() ... cfgparser.optionxform = str Note that when reading configuration files, whitespace around the option names are stripped before :meth:`optionxform` is called. .. _configparser-objects: ConfigParser Objects -------------------- The :class:`~ConfigParser.ConfigParser` class extends some methods of the :class:`RawConfigParser` interface, adding some optional arguments. .. method:: ConfigParser.get(section, option[, raw[, vars]]) Get an *option* value for the named *section*. If *vars* is provided, it must be a dictionary. The *option* is looked up in *vars* (if provided), *section*, and in *defaults* in that order. All the ``'%'`` interpolations are expanded in the return values, unless the *raw* argument is true. Values for interpolation keys are looked up in the same manner as the option. .. method:: ConfigParser.items(section[, raw[, vars]]) Return a list of ``(name, value)`` pairs for each option in the given *section*. Optional arguments have the same meaning as for the :meth:`get` method. .. versionadded:: 2.3 .. _safeconfigparser-objects: SafeConfigParser Objects ------------------------ The :class:`SafeConfigParser` class implements the same extended interface as :class:`~ConfigParser.ConfigParser`, with the following addition: .. method:: SafeConfigParser.set(section, option, value) If the given section exists, set the given option to the specified value; otherwise raise :exc:`NoSectionError`. *value* must be a string (:class:`str` or :class:`unicode`); if not, :exc:`TypeError` is raised. .. versionadded:: 2.4 Examples -------- An example of writing to a configuration file:: import ConfigParser config = ConfigParser.RawConfigParser() # When adding sections or items, add them in the reverse order of # how you want them to be displayed in the actual file. # In addition, please note that using RawConfigParser's and the raw # mode of ConfigParser's respective set functions, you can assign # non-string values to keys internally, but will receive an error # when attempting to write to a file or when you get it in non-raw # mode. SafeConfigParser does not allow such assignments to take place. config.add_section('Section1') config.set('Section1', 'an_int', '15') config.set('Section1', 'a_bool', 'true') config.set('Section1', 'a_float', '3.1415') config.set('Section1', 'baz', 'fun') config.set('Section1', 'bar', 'Python') config.set('Section1', 'foo', '%(bar)s is %(baz)s!') # Writing our configuration file to 'example.cfg' with open('example.cfg', 'wb') as configfile: config.write(configfile) An example of reading the configuration file again:: import ConfigParser config = ConfigParser.RawConfigParser() config.read('example.cfg') # getfloat() raises an exception if the value is not a float # getint() and getboolean() also do this for their respective types a_float = config.getfloat('Section1', 'a_float') an_int = config.getint('Section1', 'an_int') print a_float + an_int # Notice that the next output does not interpolate '%(bar)s' or '%(baz)s'. # This is because we are using a RawConfigParser(). if config.getboolean('Section1', 'a_bool'): print config.get('Section1', 'foo') To get interpolation, you will need to use a :class:`~ConfigParser.ConfigParser` or :class:`SafeConfigParser`:: import ConfigParser config = ConfigParser.ConfigParser() config.read('example.cfg') # Set the third, optional argument of get to 1 if you wish to use raw mode. print config.get('Section1', 'foo', 0) # -> "Python is fun!" print config.get('Section1', 'foo', 1) # -> "%(bar)s is %(baz)s!" # The optional fourth argument is a dict with members that will take # precedence in interpolation. print config.get('Section1', 'foo', 0, {'bar': 'Documentation', 'baz': 'evil'}) Defaults are available in all three types of ConfigParsers. They are used in interpolation if an option used is not defined elsewhere. :: import ConfigParser # New instance with 'bar' and 'baz' defaulting to 'Life' and 'hard' each config = ConfigParser.SafeConfigParser({'bar': 'Life', 'baz': 'hard'}) config.read('example.cfg') print config.get('Section1', 'foo') # -> "Python is fun!" config.remove_option('Section1', 'bar') config.remove_option('Section1', 'baz') print config.get('Section1', 'foo') # -> "Life is hard!" The function ``opt_move`` below can be used to move options between sections:: def opt_move(config, section1, section2, option): try: config.set(section2, option, config.get(section1, option, 1)) except ConfigParser.NoSectionError: # Create non-existent section config.add_section(section2) opt_move(config, section1, section2, option) else: config.remove_option(section1, option) Some configuration files are known to include settings without values, but which otherwise conform to the syntax supported by :mod:`ConfigParser`. The *allow_no_value* parameter to the constructor can be used to indicate that such values should be accepted: .. doctest:: >>> import ConfigParser >>> import io >>> sample_config = """ ... [mysqld] ... user = mysql ... pid-file = /var/run/mysqld/mysqld.pid ... skip-external-locking ... old_passwords = 1 ... skip-bdb ... skip-innodb ... """ >>> config = ConfigParser.RawConfigParser(allow_no_value=True) >>> config.readfp(io.BytesIO(sample_config)) >>> # Settings with values are treated as before: >>> config.get("mysqld", "user") 'mysql' >>> # Settings without values provide None: >>> config.get("mysqld", "skip-bdb") >>> # Settings which aren't specified still raise an error: >>> config.get("mysqld", "does-not-exist") Traceback (most recent call last): ... ConfigParser.NoOptionError: No option 'does-not-exist' in section: 'mysqld'