The Configparser module of the Python Standard library provides a set of APIs to read and manipulate configuration files.
Format of the configuration file
A) The configuration file contains one or more sections, each section has its own option;
b) section is denoted by [Sect_name] , where each option is a key-value pair, using delimiter = or : separated;
c) Spaces at the end of the option delimiter are ignored
d) config file using # and; Comments
A simple sample configuration file myapp.conf
# database Source
[db]
Host = 127.0.0.1
Port = 3306
user = root
pass = root
# SSH
[SSH]
Host = 192.168.1.101
user = Huey
Pass = Huey
Basic operation of Configparser
A) instantiate the Configparser and load the configuration file
CP = Configparser.safeconfigparser () cp.read ('myapp.conf')
b) Get the section list, option key list, and option key value tuple list
Print 'All sections:', Cp.sections ()#sections: [' db ', ' ssh ']Print 'options of [db]:', Cp.options ('DB')#options of [db]: [' host ', ' Port ', ' user ', ' Pass ']Print 'items of [SSH]:', Cp.items ('SSH')#items of [SSH]: [(' Host ', ' 192.168.1.101 '), (' User ', ' Huey '), (' Pass ', ' Huey ')]
c) Read the specified configuration information
Print 'host of DB:', Cp.get ('DB','Host')#host of db:127.0.0.1Print 'host of SSH:', Cp.get ('SSH','Host')#host of Ssh:192.168.1.101
d) Read configuration information by type: Getint, GetFloat, and Getboolean
print type (cp.getint ('db'port')) # <type ' int ' >
e) determine if option exists
print cp.has_option ('db'host') # True
f) Set option
Cp.set ('db'host',' 192.168.1.102')
g) Delete option
Cp.remove_option ('db'host')
h) Determine if a section exists
print cp.has_section ('db') # True
i) Add section
Cp.add_section ('new_sect')
j) Delete section
Cp.remove_section ('db')
k) Save configuration, set, Remove_option, Add_section, and remove_section do not modify the configuration file, the Write method can write the configuration of the Configparser object to the file
Cp.write (Open ('myapp.conf'w')) Cp.write (sys.stdout)
Allow_no_value
Typically, option is a key-value pair. However, when the Safeconfigparser parameter allow_no_value is set to True, it allows option to not set the value but only as an identity.
Allow_no_value.conf
# option as Flag
[Flag]
Flag_opt
allow_no_value.py
Import= configparser.safeconfigparser (Allow_no_value = True) cp.read (' myapp.conf')print cp.get ('flag' flag_opt'); # None
Allow_no_value is set to False by default, if there is an option in the configuration file that does not have a value set, an exception configparser.parsingerror is thrown when the configuration file is read. When Allow_no_value is set to True, if an option is not set the value, the Has_option method returns the True,get method returns None.
DEFAULT section
If there is a section in the configuration file named DEFAULT, then the other sections extend its option and can override its option.
Db.conf
[default]host = 127.0.0.1port = 3306[db_root]user = Rootpass = Root[db_huey]host = 192.168.1.101user = Hueypass = Huey
default_section.py
print cp.get ('db_root'host') # 127.0.0.1Print cp.get ('db_huey'host ') # 192.168.1.101
interpolation interpolation
Safeconfigparser provides interpolation features to combine data.
Url.conf
=% (protocol) s://% (server) s:% (port) s/== = 8080=% (protocol) s://% (server) s/ == 192.168.1.102
interpolation_demo.py
ImportCONFIGPARSERCP=Configparser.safeconfigparser () cp.read ('url.conf')PrintCp.get ('http','URL')#http://localhost:8080/PrintCp.get ('FTP','URL')#ftp://192.168.1.102/
For more configparser use, refer to:
- Http://pymotw.com/2/ConfigParser/index.html
- Https://docs.python.org/2/library/configparser.html
Python (2.7.6) configparser-read-write configuration file