python config文件的读写操作示例
作者:以梦为马越骑越傻 时间:2022-04-05 07:42:11
本文实例讲述了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
标签:python,config文件,读写操作
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
原创一个AJAX类
2008-07-24 13:29:00
FrontPage2002简明教程四:网页超级链接
2008-09-17 11:23:00
![](https://img.aspxhome.com/file/UploadPic/200810/17/20081017113025456s.jpg)
Python matplotlib如何简单绘制不同类型的表格
2021-10-16 12:23:48
![](https://img.aspxhome.com/file/2023/7/75887_0s.png)
根据表中数据生成insert语句的存储过程
2008-11-10 12:13:00
一文详解Python灰色预测模型实现示例
2023-05-11 01:45:21
![](https://img.aspxhome.com/file/2023/6/76126_0s.png)
纯ASP结合VML生成完美图-饼图
2010-05-11 16:49:00
python下如何查询CS反恐精英的服务器信息
2021-09-15 05:51:47
django项目简单调取百度翻译接口的方法
2023-10-20 18:25:41
![](https://img.aspxhome.com/file/2023/0/64280_0s.png)
web程序员的思考
2009-08-04 13:10:00
JS中setTimeout()的用法详解
2023-06-26 20:55:17
![](https://img.aspxhome.com/file/2023/6/55876_0s.gif)
Golang嵌入资源文件实现步骤详解
2023-06-21 08:52:36
python打印9宫格、25宫格等奇数格 满足横竖斜相加和相等
2023-08-27 07:55:11
![](https://img.aspxhome.com/file/2023/7/72977_0s.jpg)
微信小程序实现图片上传功能实例(前端+PHP后端)
2023-11-05 14:19:27
![](https://img.aspxhome.com/file/2023/8/56278_0s.jpg)
Python采集王者最低战力信息实战示例
2021-09-29 17:54:13
![](https://img.aspxhome.com/file/2023/9/72949_0s.png)
用javascript结合Cookies记录浏览历史
2008-02-12 12:37:00
图文详解laravel多对多关联模型
2023-06-12 22:21:34
![](https://img.aspxhome.com/file/2023/4/55544_0s.png)
asp如何远程注册DLL
2010-06-16 09:58:00
索引在Oracle中的应用深入分析
2023-07-04 19:50:49
用Javascript正则表达式验证Email地址
2009-12-09 15:56:00
如何把ACCESS转成SQL数据库
2007-08-11 13:51:00