.tel域名不可以做网站域名吗,云南协千,网站推广托管,购物网站开发问题域分析在程序开发中#xff0c;使用独立的配置文件来配置一些参数常见且方便#xff0c;配置文件的解析或修改并不复杂#xff0c;在python里更是如此#xff0c;在官方发布的库中就包含有做这件事情的库#xff0c;那就是ConfigParser#xff0c;ConfigParser模块解析的配置文…在程序开发中使用独立的配置文件来配置一些参数常见且方便配置文件的解析或修改并不复杂在python里更是如此在官方发布的库中就包含有做这件事情的库那就是ConfigParserConfigParser模块解析的配置文件的格式类似ini的配置文件格式就是文件由多个section构成每个section下又有多个配置项.
下面就以一个具体的脚本实例来说明这个模块的使用方法:
脚本文件名chconfig.py 代码如下:
#!/usr/bin/python
# -*- coding:gbk -*-
# Author: Droeny.zhao
# Version: 2010-04-08
import sys
import ConfigParser
def getinfo(COLUMN,ITEM):
confConfigParser.ConfigParser()
CONFIGNAME game.ini
if os.path.isfile(CONFIGNAME):
conf.read(CONFIGNAME)
conf.sections()
try:
return conf.get(COLUMN,ITEM)
except ConfigParser.NoOptionError, e:
print Wanning:,e
sys.exit()
else:
print Wanning: %s is not exists, you must appoint the absolute path of config file with -p or -c.%(CONFIGNAME)
sys.exit()
def setinfo(COLUMN,ITEM,VALUE):
confConfigParser.ConfigParser()
CONFIGNAME game.ini
if os.path.isfile(CONFIGNAME):
conf.read(CONFIGNAME)
conf.sections()
try:
conf.set(COLUMN,ITEM,VALUE)
conf.write(open(CONFIGNAME, w))
except ConfigParser.NoOptionError, e:
print Wanning:,e
sys.exit()
else:
print Wanning: %s is not exists, you must appoint the absolute path of config file with -p or -c.%(CONFIGNAME)
sys.exit()
if __name__ __main__:
print getinfo(Account,DBName)
setinfo(Account,DBName,GameDB)
print getinfo(Account,DBName)
配置文件game.ini 代码如下:
[Account]
username test01
servername 192.168.0.1
password 123456
dbname AccountDB
[Database]
username test02
servername 192.168.0.2
password 123456
dbname GameDB