Property Files and Configuration (or
A property file, also known as a configuration (or
.INI) file defines property or configuration
values. It is usually just a collection of settings. The essential
property-file format has a simple row-oriented format with only two
values in each row. A configuration (or
organizes a simple list of properties into one or more named
A property file uses a few punctuation rules to encode the
Lines begining with
ignored. In some dialects the comments are
Each property setting is delimited by an ordinary newline
character. This is usually the standard
\n. If you are
exchanging files across platforms, you may need to open files for
reading using the "rU" mode to get universal newline
Each property is a simple name and a value. The name is a
string characters that does not use a separator character of
=. The value is everything after the
punctuation mark, with leading and trailing spaces removed. In some
dialects space is also a separator character.
Some property file dialects allow a value to continue on to the
next line. In this case, a line that ends with
\n) escapes the usual
\n. Rather being the end of a line,
\\n is just another whitespace character.
A property file is an extension to the basic tab-delimited file.
It has just two columns per line, and some space-stripping is done.
However, it doesn't have a consistent separator, so it is slightly more
complex to parse.
The extra feature introduced in a configuration file is named
Reading a Simple Property File. Here's an example of reading the simplest kind of property file.
In this case, we'll turn the entire file into a dictionary. Python
doesn't provide a module for doing this. The processing is a sequence
string manipulations to parse the file.
propFile= file( r"C:\Java\jdk1.5.0_06\jre\lib\logging.properties", "rU" )
for propLine in propFile:
if len(propDef) == 0:
if propDef in ( '!', '#' ):
punctuation= [ propDef.find(c) for c in ':= ' ] + [ len(propDef) ]
found= min( [ pos for pos in punctuation if pos != -1 ] )
value= propDef[found:].lstrip(":= ").rstrip()
The input line is subject to a number of processing steps.
First the leading and trailing whitespace is removed. If the
line is empty, nothing more needs to be done.
If the line begins with
; in some dialects) it is ignored.
We find the location of all relevant puncuation marks. In some
dialects, space is not permitted. Note that we through the length of
the line on the end to permit a single word to be a valid property
name, with an implicit value of a zero-length string.
By discarding punction positions of -1, we are only processing
the positions of punctuation marks which actually occur in the
string. The smallest of these positions is the left-most punctuation
The name is everything before the punctuation mark with
The value is everything after the punctuaion mark. Any
additional separators are removed, and any trailing whitespace is
Reading a Config File. The
ConfigParser module has a number of
classes for processing configuration files. You initialize a
ConfigParse object with default values. The object can the read one or
more a configuration files. You can then use methods to determine what
sections were present and what options were defined in a given
cp= ConfigParser.RawConfigParser( )
cp.read( r"C:\Program Files\Mozilla Firefox\updater.ini" )
Eschewing Obfuscation. While a property file is rather simple, it is possible to
simplify property files further. The essential property definition
syntax is so close to Python's own syntax that some applications use a
simple file of Python variable settings. In this case, the settings
file would look like this.
Example 34.3. settings.py
# Some Properties
TITLE = "The Title String"
INFO = """The information string.
Which uses Python's ordinary techniques
for long lines."""
This file can be introduced in your program with one statement:
import settings. This statement will create module-level