When doing development in Python will often use the database or other dynamic configuration of things, hard-coded in the inside every time to change will be very troublesome.Python comes with a module to read the configuration file ConfigParser, very convenient to use.
ini files
ini configuration file format:
Read the configuration file:
import ConfigParser conf = () ('') # File path name = ("section1", "name") # Get the option value for the specified section. print name sex = ("section1", "sex") # Get the sex value of section1. print age
Output:
jhao male
Write to the configuration file:
import ConfigParser conf = () ('') ("section1", "name", "jhao104") # Modify the option of the specified section ("section1", "age", "21") # Add an option to the specified section conf.add_section("section3") # Add sections ("section3", "site", "") # Write an option to the added section (open('', 'w'))
Output: