Python之简洁ini读写

ini文件是常用的配置文件格式,当然,除了它之外,还有其他好多种,比如conf,xml等等。不过,今天就谈论ini罢了。

python对此提供了相应的模块,示例如下:

 

#coding=utf-8

import ConfigParser

def writeConfig(filename):
	config = ConfigParser.ConfigParser()
	# set db
	section_name = 'db'
	config.add_section( section_name  )
	config.set( section_name, 'dbname', 'MySQL')
	config.set( section_name, 'host', '127.0.0.1')
	config.set( section_name, 'port', '80')
	config.set( section_name, 'password', '123456')
	config.set( section_name, 'databasename', 'test')
	
	# set app
	section_name = 'app'
	config.add_section( section_name  )
	config.set( section_name, 'loggerapp', '192.168.20.2')
	config.set( section_name, 'reportapp', '192.168.20.3')
	
	# write to file
	config.write( open(filename, 'a') )
	
def updateConfig(filename, section, **keyv):
	config = ConfigParser.ConfigParser()
	config.read(filename)
	[config.set(section, key, keyv[key]) for key in keyv if config.has_option(section, key)]
	config.write( open(filename, 'r+') )
	
if __name__ == '__main__':
	file_name = 'test.ini'
	writeConfig(file_name)
	updateConfig(file_name, 'app', reportapp = '192.168.100.100')
	print "end__"

你可能感兴趣的:(xml,mysql,python,File,ini,import)