浙江省高等学校教师教育理论培训

微信搜索“毛凌志岗前心得”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

sudo easy_install dict4ini

 

ConfigParser模块是python自带的读取配置文件的模块.通过他可以方便的读取配置文件. 这篇文章简单介绍一下读取配置文件的方法.

配置文件.顾名思议就是存放配置的文件.下面是个例子
[info]
age = 21
name = chen
sex = male

其中[ ] 中的info是这段配置的名字
下面age,name都是属性

下面的代码演示了如何读取配置文件.和修改配置中变量的值

from __future__ import with_statement
import ConfigParser
config
=ConfigParser.ConfigParser()
with open('testcfg.cfg','rw') as cfgfile:
    config.readfp(cfgfile)
    name
=config.get('info','name')
    age
=config.get('info','age')
    
print name
    
print age
    config.set(
'info','sex','male')
    config.set(
'info','age','21')
    age
=config.get('info','age')
    
print name
    
print age

posted on 2010-09-24 09:00  lexus  阅读(269)  评论(0)    收藏  举报