Pythonでiniファイルの読み書き方法

サンプルコード:
#coding=utf-8
import ConfigParser

def writeConfig(filename):
config = ConfigParser.ConfigParser()
# 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’, 'startnews24’)
config.set( section_name, 'databasename’, 'startnews24_test’)

# 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’)

# ファイルへの書き込み
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()
# 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’, 'startnews24’)
config.set( section_name, 'databasename’, 'startnews24_test’)

#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’)

# ファイルへの書き込み
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__"

Development

Posted by arkgame