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 ...
随机推荐
- UEditor使用说明
页面加入ue<!-- umeditor --><script type="text/javascript" charset="utf-8" s ...
- gdb汇编调试
GDB调试汇编堆栈分析 代码: sudo apt-get install libc6-dev-i386命令安装所需库 进入之后先在main函数处设置一个断点,再run一下,使用disassemble指 ...
- 使用archlinux作为日常开发机已经半年了,随便写一下
机器配置 CPU: Intel Core i5-6200U CPU @ 2.8GHz RAM: 6114MiB / 7421MiB Resolution: 1920x2160 在arch下常用的软件 ...
- Android7.0 新特性
删除了三个隐式广播 1.监听网络变化的广播 CONNECTIVITY_ACTION,在Manifest.xml中注册的 不会受到,在前台运行的程序 registerReceiver()注册的广播可以接 ...
- 2、python,for..in语句
for..in语句是循环语句,它迭代一个对象的序列,例如经历序列中的第一项.一个序列只是一个有序的项目的集合. for i in range(1, 5): print(i) else: print(' ...
- PV IP UV的概念介绍
IP(独立IP):指独立IP数,不同的IP地址的计算机访问网站的总次数,这个是网站流量分析的一个重要指标.00:00-24:00内相同的IP地址只被计算一次 假如说:赶集网的日独立IP300W,则至少 ...
- jQuery iframe之间相互调用
,子iframe内调用父类函数方法: window.parent.func(); ,子Iframe中获取父界面的元素: $("#xx", window.parent.documen ...
- 在sql server中利用with as实现递归功能
在sqlserver2005之前,要实现递归功能比较麻烦,比如可能会要用到临时表与while语句来循环.自sqlserver2005之后,新增了with as功能语法,即 公用表达式(CTE),让递归 ...
- 安装XMind
XMind是一款思维导图软件.可编辑整理头脑中的想法,以使其脉络更加清晰. 在学习知识过程中,用这个工具也不错. 官方网站: http://www.xmind.net 其它版本: http://ww ...
- Google V8编程详解附录
Google V8编程详工具函数 头文件:utils.h #ifndef UTILS_H_ #define UTILS_H_ #include "v8.h" #include &l ...