python confingparser与optparser

来源:互联网 发布:关于编程的纪录片 编辑:程序博客网 时间:2024/05/16 13:05

db.conf文件

[cd]host = 127.0.0.1port = 2206username = rootpassword = rootcharset = utf-8

optpar.py

from  ConfigParser import   ConfigParserimport MySQLdb as dbfrom optparse import OptionParserif __name__=="__main__":    parser = OptionParser() #---处理命令行参数    parser.add_option('-c', '--conf', dest='conf')    (options, args) = parser.parse_args()    print options,args    co=ConfigParser()    co.read(options.conf)    s=co.items('cd')    print s    db=co.get('cd','host')    po=co.get('cd','port')    co.set('cd','port','2206')    co.write(open(options.conf,'w'))    print db,po


结果




原创粉丝点击