当前第2页 返回上一页
设定配置文件test2.conf
1 2 3 4 | [portal]
url = http: // %(host)s:%(port)s /Portal
host = localhost
port = 8080
|
使用RawConfigParser:
1 2 3 4 5 6 7 8 9 10 11 | import ConfigParser
cf = ConfigParser.RawConfigParser()
print "use RawConfigParser() read"
cf.read( "test2.conf" )
print cf.get( "portal" , "url" )
print "use RawConfigParser() write"
cf. set ( "portal" , "url2" , "%(host)s:%(port)s" )
print cf.get( "portal" , "url2" )
|
得到终端输出:
1 2 3 4 | use RawConfigParser() read
http: // %(host)s:%(port)s /Portal
use RawConfigParser() write
%(host)s:%(port)s
|
改用ConfigParser:
1 2 3 4 5 6 7 8 9 10 11 | import ConfigParser
cf = ConfigParser.ConfigParser()
print "use ConfigParser() read"
cf.read( "test2.conf" )
print cf.get( "portal" , "url" )
print "use ConfigParser() write"
cf. set ( "portal" , "url2" , "%(host)s:%(port)s" )
print cf.get( "portal" , "url2" )
|
得到终端输出:
1 2 3 4 | use ConfigParser() read
http: //localhost :8080 /Portal
use ConfigParser() write
localhost:8080
|
改用SafeConfigParser:
1 2 3 4 5 6 7 8 9 10 11 | import ConfigParser
cf = ConfigParser.SafeConfigParser()
print "use SafeConfigParser() read"
cf.read( "test2.conf" )
print cf.get( "portal" , "url" )
print "use SateConfigParser() write"
cf. set ( "portal" , "url2" , "%(host)s:%(port)s" )
print cf.get( "portal" , "url2" )
|
得到终端输出(效果同ConfigParser):
1 2 3 4 | use SafeConfigParser() read
http: //localhost :8080 /Portal
use SateConfigParser() write
localhost:8080
|
以上就是Python之ConfigParser配置文件详解的详细内容,更多文章请关注木庄网络博客!!
返回前面的内容
相关阅读 >>
windows上使用Python增加或删除权限的方法
Python global用法有哪些?
Python中可迭代对象分解为单独的变量的实现方法(代码)
如何在ubuntu中安装Python 3.6?
Python中import有什么用法
Python实现的端口扫描功能
Python anaconda 安装 环境变量 升级 以及特殊库安装
Python如何导入图片
Python针对含中文字符串的截取功能的实现示例
windows下安装Python的教程
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » Python之ConfigParser配置文件详解