It is common to use a configuration file in a program to flexibly configure some parameters. The parsing of configuration files is not complicated, especially in Python, the officially released library contains the library for doing this, that is, ConfigParser. Here is a brief introduction. The format of the configuration file parsed by the Python ConfigParser module is similar to that of the ini configuration file, which consists of multiple sections. Each section has multiple configuration items, such: [db] db_host = 192.168.1.1 db_port = 3306 db_user = root db_pass = password [concurrent] thread = 200 processor = 400 suppose the name of the above configuration file is test. conf. It contains two sections, one is db, the other is concurrent, the db also contains four items, and the concurrent contains two items. Here we will do the parsing: #-*-encoding: gb2312-*-import ConfigParser, string, OS, sys cf = ConfigParser. configParser () cf. read ("test. conf ") # return all sections s = cf. sections () print 'section: ', s o = cf. options ("db") print 'Options: ', o v = cf. items ("db") print 'db: ', v print'-'* 60 # db_host = cf. get ("db", "db_host") db_port = cf. getint ("db", "db_port") db_user = cf. get ("db", "db_user") db_pass = cf. get ("db "," Db_pass ") # returns the integer threads = cf. getint ("concurrent", "thread") processors = cf. getint ("concurrent", "processor") print "db_host:", db_host print "db_port:", db_port print "db_user:", db_user print "db_pass :", db_pass print "thread:", threads print "processor:", processors # modify a value and write it back to cf. set ("db", "db_pass", "zhaowei") cf. write (open ("test. conf "," w ") # Add a section. (Also write back) cf. add_section ('liuqing') cf. set ('liuqing', 'int', '15') cf. set ('liuqing', 'bool ', 'true') cf. set ('liuqing', 'float', '3. 1415 ') cf. set ('liuqing', 'baz', 'fun ') cf. set ('liuqing', 'bar', 'python') cf. set ('liuqing', 'foo', '% (bar) s is % (baz) s! ') Cf. write (open ("test. conf", "w") # remove section or option. (Write it back after modification.) cf. remove_option ('liuqing ', 'int') cf. remove_section ('liuqing') cf. write (open ("test. conf "," w ") The above is an introduction to the application methods of the Python ConfigParser module. Of course, this module has many other usages. If you are interested, go to the official website to see: http://docs.python.org/2/library/configparser.html.