本文实例讲述了python config文件的读写操作。分享给大家供大家参考,具体如下:
1、设置配置文件
[mysql]
host = 1234
port = 3306
user = root
password = Zhsy08241128
database = leartd
2、读取配置文件
import configparser
import os
conf= configparser.ConfigParser()
def readConf():
'''读取配置文件'''
root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
conf.read(root_path + '/ceshi/conf/app.conf') # 文件路径
print(conf)
name = conf.get("mysql", "host") # 获取指定section 的option值
print(name)
3、写入配置文件
def writeConf():
'''写入配置文件'''
root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
conf.read(root_path + '/ceshi/conf/app.conf') # 文件路径
conf.set("mysql", "host", "1234") # 修改指定section 的option
conf.write(open(root_path + '/ceshi/conf/app.conf', 'w'))
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python函数使用技巧总结》、《Python面向对象程序设计入门与进阶教程》、《Python数据结构与算法教程》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
本文标题: python config文件的读写操作示例
本文地址: http://www.cppcns.com/jiaoben/python/276792.html