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. Dev ChartControl鼠标移动显示坐标点

    his.chartIRC_RATES.MouseMove += new MouseEventHandler(chartIRC_RATES_MouseMove); ToolTipController t ...

  2. Django URL的命令空间

    为避免在模板中使用URL的硬编码,可以使用{% url %}模板标签来解决 <li><a href="/task/{{ task.id }}/">{{ ta ...

  3. 关于angularJS绑定数据时自动转义html标签

    关于angularJS绑定数据时自动转义html标签 angularJS在进行数据绑定时默认是会以文本的形式输出,也就是对你数据中的html标签不进行转义照单全收,这样提高了安全性,防止了html标签 ...

  4. Flex使用Blazeds与Java交互及自定义对象转换详解-DATAGRID读取ORACLE数据

    http://www.cnblogs.com/RocD-DuPeng/articles/1751040.html 一.建立Flex与Java交互的工程. 本文中讲到的交互是利用Blazeds的,因为这 ...

  5. Ubuntu实现wifi热点

    系统:Ubuntu14.04 方法一: 打开网络连接,新建wifi,设置SSID和密码,完成设置后保存,再连接到隐藏的wifi,选择刚建立好的连接即可 优点:简单方便 缺点:建立的是Ad-hoc模式信 ...

  6. java正则随笔

    一.string校验 要求字符串只能输入数字,字母大小写和‘@‘’‘.’‘_’三个特殊字符 public static boolean check1(String str){ String patte ...

  7. HP_UX HBA 卡信息收集脚本

    #/usr/bin/shname1=`hostname`_fcioscan -kfnNC fc |grep "/dev/" >/tmp/data/$name1.txtnum1 ...

  8. 【230】4T硬盘如何完全利用(GPT)

    参考:如何选择超过2T和3T及以上硬盘的MBR与GPT分区形式 新购置的硬盘是 4TB 的,装上后只能用 2TB 的,查明后得知是因为 MBR 只支持 2TB 的(默认情况下是 MBR 分区形式的), ...

  9. sniffer底层网络配置记录

    公司需要一款可以检查网络的软件,之后任务分配到我们组 经过讨论最终方案是以wireshark为版本,进行二次开发,主要目的在于简化上层操作复杂性(软件使用人群为非专业性的) 软件分为三部分,其一底层网 ...

  10. SRM 146 DIV1 600

    Problem Statement      Masterbrain is a two player board game in which one player decides on a secre ...