网络编程
位置:首页>> 网络编程>> Python编程>> python config文件的读写操作示例

python config文件的读写操作示例

作者:以梦为马越骑越傻  发布时间:2022-04-05 07:42:11 

标签:python,config文件,读写操作

本文实例讲述了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程序设计有所帮助。

来源:https://blog.csdn.net/weixin_42670402/article/details/89647495

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com