Python configparser修改配置文件
来源:互联网 发布:mac最新系统版本10.13 编辑:程序博客网 时间:2024/05/22 10:22
公司的产品部署在Linux服务器上,安装好后需要在配置文件中设置许多性能参数以达到最佳performance. Python中的configparser可以很方便的修改config文件,将调整好的参数写在一个config文件中,每次新安装产品后解析config文件,将对应的参数写入系统的config文件。
以下是示意:
模版文件 test1.conf内容如下:
[s1]size=800len=400[s2]user=rootpasswd=root
需要将test2.conf中的参数值改为test1.conf中的值。
test2.conf内容如下:
[s1]size=1000len=500[s2]user=passwd=[s3]size=150count=3
Python脚本:
import configparsercp1 = configparser.ConfigParser()cp2 = configparser.ConfigParser()cp1.read('test1.conf')cp2.read('test2.conf')sections = cp1.sections()for section in sections: """ print(section) print(cp.items(section)) print(cp.items(section)[0][0])""" print(section, cp1.items(section)[0][0], cp1.items(section)[0][1]) cp2.set(section, cp1.items(section)[0][0], cp1.items(section)[0][1])with open("test2.conf", "w+") as f: cp2.write(f)
执行脚本,test2.conf修改为:
[s1]size=800len=400[s2]user=rootpasswd=root[s3]size=150count=3
阅读全文
0 0
- Python configparser修改配置文件
- python 中使用ConfigParser类修改配置文件
- python 使用ConfigParser解析&修改配置文件
- Python读取修改ini配置文件[ConfigParser]
- PYTHON 配置文件模块 ConfigParser
- [Python]ConfigParser解析配置文件
- python ConfigParser操作配置文件
- 【python】ConfigParser---读写配置文件
- python读取配置文件configparser
- 【python】configparser读写配置文件
- Python 配置文件 ConfigParser 模块
- python-ConfigParser模块【读写配置文件】
- Python利用ConfigParser读取配置文件
- 【ConfigParser模块】Python处理配置文件
- Python 之 ConfigParser : 配置文件读取
- python-ConfigParser模块【读写配置文件】
- python-ConfigParser模块【读写配置文件】
- Python利用ConfigParser读取配置文件
- Android Studio 快捷键
- 打动人的瞬间·《毛骗》
- JAVA线程间通信的几种方式
- 【HPU OJ 1409 】Watch Dog 【最小生成树】
- Servlet示例项目
- Python configparser修改配置文件
- git常用命令总结
- UVa10285 Longest Run on a Snowboard
- 如何实现支持大文件的高性能HTTP文件上传服务器
- 剖析JAVA虚拟机加载类的方式
- ubuntu安装Python3.5
- VMware vSphere Web Services SDK编程指南(七)- 7.4 PropertyCollector 示例 (RetrievePropertiesEx)
- poj 1325 挺好的转化。
- [操作系统]--程序,进程与线程