configparser配置文件操作
configparser 模块用于对配置操作
官方文档地址https://docs.python.org/3/library/configparser.html
导入configparser模块
import configparser
基本的读取配置文件
-read(filename) 直接读取ini文件内容
-sections() 得到所有的section,并以列表的形式返回
-options(section) 得到该section的所有option
-items(section) 得到该section的所有键值对
-get(section,option) 得到section中option的值,返回为string类型
-getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。
基本的写入配置文件
-add_section(section) 添加一个新的section
-set( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件。
配置实例
conf = configparser.Configparser() #读取配置文件
conf.read('settings.conf') try: conf.add_section('WebServer') #设置section中的option
conf.set("WebServer","IP1","192.168.3.4")
conf.set("WebServer","IP2","192.168.3.9") except configparser.DuplicateSectionError:
print("Section 'WebServer' already exists")
#添加 section
try: #添加的section
conf.add_section('FTP')
conf.set('FTP','IP1',"192.168.3.4") except configparser.DuplicateOptionError:
print("Section 'FTP'already exists") #写入配置文件
conf.write(open('group_settings.conf','w')) #获取每个 section下option的长度
l = len(conf.options('WebServer'))
z = len(conf.options('FTP')) #输出option的的值
for i in range(1,l+1):
print(conf.get("WebServer","ip%i" %i)) for i in range(1,z+1):
print(conf.get("FTP","ip%i" %i))
configparser配置文件操作的更多相关文章
- s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译
时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...
- python接口自动化测试 - configparser配置文件解析器详细使用
configparser简介 ConfigParser模块已在Python 3中重命名为configparser 该模块定义了ConfigParser类. ConfigParser类实现一种基本的配置 ...
- 配置文件操作(ini、cfg、xml、config等格式)
配置文件的格式主要有ini.xml.config等,现在对这些格式的配置文件的操作(C#)进行简单说明. INI配置文件操作 调用系统函数GetPrivateProfileString()和Write ...
- C# 配置文件操作类
注意添加引用:System.Configuration: using System; using System.Collections.Generic; using System.Text; usin ...
- Python模块之: ConfigParser 配置文件读取
Python模块之: ConfigParser 配置文件读取 ConfigParser用于读写类似INI文件的配置文件,配置文件的内容可组织为组,还支持多个选项值(option-value)类型. ...
- 修改SolrCloud在ZooKeeper中的配置文件操作记录
修改SolrCloud在ZooKeeper中的配置文件操作记录. 命令执行目录: /opt/solr-/server/scripts/cloud-scripts/ 1.下载配置文件 ./zkcli., ...
- configparser 配置文件模块
#_author:star#date:2019/11/7# configparser 配置文件模块import configparserconfig=configparser.ConfigParser ...
- logging 日志模块 configparser 配置文件
logging 模块 (copy博客) 详情浏览:http://www.cnblogs.com/linhaifeng/articles/6384466.html#_label12 函数式简单配置 im ...
- .NET程序配置文件操作(ini,cfg,config)
在程序开发过程中,我们一般会用到配置文件来设定一些参数.常见的配置文件格式为 ini, xml, config等. INI .ini文件,通常为初始化文件,是用来存储程序配置信息的文本文件. [Log ...
随机推荐
- Oracle:分割字符串 取TOP N条记录
oracle数据库,表数据如下: ids id 3,4,5 7 13,14,15,16 ...
- 新手上路,配置阿里云CentOS服务器LAMP
最近出了个9.9包月的阿里云一直想弄就买了个半年的,脑子一热选了个CentOS系统的然后就是无尽的代码...... 话不多说直接上步骤: 在实例中选择"连接管理终端",输入一些应 ...
- 移动端网页fixed布局问题解决方案
问题说明 移动端web的footer常常设计为fixed布局,但是在页面键盘被拉起时fixed的布局会出现问题,自己试了下,在较低版本ios和部分安卓机上会有此问题.具体问题看图示: <body ...
- iOS CoreAnimate 动画实现
这里主要讲的是使用CoreAnimate实现所需的动画. 先上官网的介绍:https://developer.apple.com/library/ios/documentation/Cocoa/Con ...
- MVC中Control和View之间数据传递的方式
1:ViewBag和ViewData 具体区别不做讨论,本处只演示ViewData的具体示例: Controler代码:ViewData["Employee"] = emp; Vi ...
- About Mysql 5.7 Installation
After version of mysql 5.7, mysql increase its security level. CMD run as adminstratot cd c:/mysql/b ...
- Nginx配置文件说明
在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释: #运行用户user www-data; #启动进程,通常设置成和cpu的数量相等worker_processes 1 ...
- 答:SQLServer DBA 三十问之二:系统DB有哪些,都有什么作用,需不需要做备份,为什么;损坏了如何做还原(主要是master库)
2. 系统DB有哪些,都有什么作用,需不需要做备份,为什么:损坏了如何做还原(主要是master库): master:它包含一个系统表集合,是整个实例的中央存储库,维护登录账户,其他数据库,文件分布, ...
- MES系统学习
MES系统是当今制造型企业信息化的热点,而统一建模语言UML是面向对象建模的标准语言,在软件工程发挥着重要作用.MES系统如何进行UML建模呢,今天和大家重点讨论一下MES系统的UML建模方法,请看本 ...
- 基于springmvc和restClient的rest服务的测试
在开发中,不熟悉驱动驱动测试开发的coder往往喜欢将服务应用启动以后测试,对于GET请求可以直接在浏览器中输入URL就可以,参数非中文可以直接追加到URL后.但是,对于其他请求方式的测试必须借助相应 ...