python读写ini文件的实例代码

发布时间:2020-12-06编辑:脚本学堂
介绍一个python读取ini配置文件的代码,有需要的朋友,可以参考下。

代码如下:

#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 www.jb200.com   
    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__"  

#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__"

python读写配置文件还是很简单的,以上代码包括了读取,写入与更新,建议大家亲自动手测试下,看看效果咱样!