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配置文件操作的更多相关文章

  1. 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时间 ...

  2. python接口自动化测试 - configparser配置文件解析器详细使用

    configparser简介 ConfigParser模块已在Python 3中重命名为configparser 该模块定义了ConfigParser类. ConfigParser类实现一种基本的配置 ...

  3. 配置文件操作(ini、cfg、xml、config等格式)

    配置文件的格式主要有ini.xml.config等,现在对这些格式的配置文件的操作(C#)进行简单说明. INI配置文件操作 调用系统函数GetPrivateProfileString()和Write ...

  4. C# 配置文件操作类

    注意添加引用:System.Configuration: using System; using System.Collections.Generic; using System.Text; usin ...

  5. Python模块之: ConfigParser 配置文件读取

    Python模块之: ConfigParser 配置文件读取   ConfigParser用于读写类似INI文件的配置文件,配置文件的内容可组织为组,还支持多个选项值(option-value)类型. ...

  6. 修改SolrCloud在ZooKeeper中的配置文件操作记录

    修改SolrCloud在ZooKeeper中的配置文件操作记录. 命令执行目录: /opt/solr-/server/scripts/cloud-scripts/ 1.下载配置文件 ./zkcli., ...

  7. configparser 配置文件模块

    #_author:star#date:2019/11/7# configparser 配置文件模块import configparserconfig=configparser.ConfigParser ...

  8. logging 日志模块 configparser 配置文件

    logging 模块 (copy博客) 详情浏览:http://www.cnblogs.com/linhaifeng/articles/6384466.html#_label12 函数式简单配置 im ...

  9. .NET程序配置文件操作(ini,cfg,config)

    在程序开发过程中,我们一般会用到配置文件来设定一些参数.常见的配置文件格式为 ini, xml, config等. INI .ini文件,通常为初始化文件,是用来存储程序配置信息的文本文件. [Log ...

随机推荐

  1. Android 自定义表格显示数据

    Android 自定义TextView控件,用来组成表格方便数据的展示. 首先看一下效果 样式不是很好看,需要用的可以自己优化一下. 实现方式很简单. 1.自定义控件 MyTableTextView ...

  2. 基于mini2440的Tslib的移植

    软件平台: win7系统,虚拟机ubuntu12.04 mini2440开发板 tslib是电阻式触摸屏用于校准的一个软件库,是一个开源的程序,能够为触摸屏驱动获得的采样提供诸如滤波.去抖.校准等功能 ...

  3. 扩展easyui.datagrid,添加数据loading遮罩效果代码 --来自网摘收集

    //jquery.datagrid 扩展 (function (){ $.extend($.fn.datagrid.methods, { //显示遮罩 loading: function(jq){ r ...

  4. js正则表达式replace里有变量的解决方法用到RegExp类

    一直比较害怕使用正则表达式,貌似很深奥很复杂的样子,所以在用js操作字符串的时候,我最多使用的是replace.split.substring.indexOf等函数,这些函数有时候需要多次叠加使用,但 ...

  5. NHibernate系列文章目录

    第一章:NHibernate基础 NHibernate介绍 第一个NHibernate工程 简单的增删改查询 运行时监控 NHibernate配置 数据类型映射 Get/Load方法 NHiberna ...

  6. js学习笔记(一)

    js 有5中原始类型:number. string. boolean. null.undefined js 有6中类型: 5中原始类型 在加上1中Object类型 typeof null === 'o ...

  7. cakephp 打印出SQL语句

    最近一直在使用cakephp这个框架学习,最近发现了一些问题,就是怎样将SQL语句打印出来进行调试,方法如下: $db=ConnectionManager::getDataSource('defaul ...

  8. 超高性能的json序列化之MVC中使用Json.Net

    先不废话,直接上代码 Asp.net MVC自带Json序列化 /// <summary> /// 加载组件列表 /// </summary> /// <param na ...

  9. JS Json数据转换

    *** json字符串中不能出现单引号,不然JSON.parse会报错,处理方式将单引号转义 概述 JSON.stringify() 方法可以将任意的 JavaScript 值序列化成 JSON 字符 ...

  10. CSS3新增属性

    1>RGBA透明度(红.绿.蓝.alpha透明度) 2>块阴影 box-shadow(标签).text-shadow(文字) 3>圆角阴影 border-radius 4>边框 ...