配置文件:config.ini
[global]
country = China
language = zh-CN
[custom]
home = appblog.cn
Python脚本
# -*- coding: utf-8 -*-
import ConfigParser
import os
def set_ConfigParser(filename, section, option, value):
# 写配置
if os.path.exists(filename):
conf = ConfigParser.ConfigParser()
conf.read(filename)
set_value = conf.set(section, option, value)
conf.write(open(filename, "w+"))
else:
set_value = ''
return set_value
def get_ConfigParser(filename, section, option):
# 读配置
if os.path.exists(filename):
conf = ConfigParser.ConfigParser()
conf.read(filename)
get_value = conf.get(section, option)
else:
get_value = ''
return get_value
if __name__ == "__main__":
set_ConfigParser('config.ini', 'custom', 'name', 'Joe.Ye')
print get_ConfigParser('config.ini', 'custom', 'name')