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 ...
随机推荐
- leetcode-【中等题】228. Summary Ranges
题目: 228. Summary Ranges Given a sorted integer array without duplicates, return the summary of its r ...
- Tomcat回收连接
最近公司一个JDK1.4的老项目升级了JDK1.6后BUG不断,最可恶的连接池被占满. 因为是使用tomcat的连接池所以在config下中添加 <Resource name="jdb ...
- 阅读Python官方文档心得
我会每天都阅读一些python的官方文档,并每天更新心得体会. -------------------------------------------------2016.12.08--------- ...
- Windows IIS 安装配置PHP环境
一. 概述 二.安装PHP 1.到php官网下载最新版PHP http://windows.php.net/download 三.配置IIS PHP环境
- 17.linux下root用户与普通用户
默认安装完成之后并不知道root用户的密码,那么如何应用root权限呢? (1)sudo 命令 这样输入当前管理员用户密码就可以得到超级用户的权限.但默认的情况下5分钟root权限就失效了. (2 ...
- 图解GitHub基本操作
目录 一.注册并登陆到github网站 1.1.打开github网站首页(https://github.com/) 1.2.注册一个自己的github账号 1.3.登陆自己的github账号 二.创建 ...
- redis 事务
概述 相信学过MySQL等其他数据库的同学对事务这个词都不陌生,事务表示的是一组动作,这组动作要么全部执行,要么全部不执行.为什么会有这样的需求呢?看看下面的场景: 微博是一个弱关系型社交网络,用户之 ...
- ios10 no route to host
IdTCPClient1 ios10 no route to host IOS 9 正常 IOS10 出错,参考下面的设置解决问题 http://www.sh-huhe.com/solution/14 ...
- Quartz.NET作业调度框架详解(转)
Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不牺牲 ...
- 发现大量的TIME_WAIT解决办法
存在一定的TIME_WAIT是正常的,个人认为如果超过了连接数的比例就不是很正常 服务器端与客户端建立TCP/IP连接后关闭SOCKET后,服务器端连接的端口状态变为TIME_WAIT.主动关闭的一方 ...