python配置文件模块_python 的配置文件模块

写入配置文件:

import ConfigParser, os

#引用的模块:

config = ConfigParser.RawConfigParser()

# When adding sections or items, add them in the reverse order of

# how you want them to be displayed in the actual file.

# In addition, please note that using RawConfigParser's and the raw

# mode of ConfigParser's respective set functions, you can assign

# non-string values to keys internally, but will receive an error

# when attempting to write to a file or when you get it in non-raw

# mode. SafeConfigParser does not allow such assignments to take place.

config.add_section('Section1')

config.set('Section1', 'int', '15')

config.set('Section1', 'bool', 'true')

config.set('Section1', 'float', '3.1415')

config.set('Section1', 'baz', 'fun')

config.set('Section1', 'bar', 'Python')

config.set('Section1', 'foo', '%(bar)s is %(baz)s!')

# Writing our configuration file to 'example.cfg'

with open('example.cfg', 'wb') as configfile:

config.write(configfile)

读入这个配置文件:

import ConfigParser

config = ConfigParser.RawConfigParser()

config.read('example.cfg')

# getfloat() raises an exception if the value is not a float

# getint() and getboolean() also do this for their respective types

float = config.getfloat('Section1', 'float')

int = config.getint('Section1', 'int')

print float + int

# Notice that the next output does not interpolate '%(bar)s' or '%(baz)s'.

# This is because we are using a RawConfigParser().

if config.getboolean('Section1', 'bool'):

print config.get('Section1', 'foo')

分享到:

2009-01-14 16:41

浏览 1087

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值