from configparser import ConfigParser
fp = 'conf.ini' #定义配置文件名
conf = ConfigParser() #实例化
conf.read(fp) # 打开conf
conf.add_section('Section1') #添加conf节点
conf.set('Section1', 'name', 'jack') #添加值
conf.set('Section1', 'age', '23')
conf.set('Section1', 'worker', 'CEO')
conf.add_section('Section2') #添加conf节点
conf.set('Section2', 'name', 'rose') #添加值
conf.set('Section2', 'age', '21')
conf.set('Section2', 'worker', 'CCC')
with open(fp, 'w') as fw: #循环写入
conf.write(fw) '''
[Section1]
name = jack
age = 23
worker = CEO ''' #读取配置文件
from configparser import ConfigParser
fp = 'conf.ini' #定义配置文件名
conf = ConfigParser() #实例化
conf.read(fp) # 打开conf
name = conf.get('Section1','name')
print(name)
'''
1)读取配置文件
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() 函数。
'''
section= conf.sections()
print(section)
option =conf.options('Section1')
print(option)
item=conf.items('Section1')
print(item) #改写操作
conf.set('Section1', 'name', 'jackadam') #设置为新值
with open(fp, 'w') as fw: #循环写入
conf.write(fw)
from configparser import ConfigParser #重新读取
fp = 'conf.ini' #定义配置文件名
conf = ConfigParser() #实例化
conf.read(fp) # 打开conf
name = conf.get('Section1','name')
print(name)

  

python--ConfigParser读写改配置文件的更多相关文章

  1. Python-通过configparser读写配置文件

    Python读写配置文件: 1.创建配置文件(文件名以.conf或.ini结束的文件表示配置文件) 2.导入所需模块 OS, configparser >>> import os & ...

  2. ConfigParser 读写配置文件

    一.ini: 1..ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式 2.ini文件创建方法: (1)先建立一个记事本文件.(2 ...

  3. Python 用configparser读写ini文件

    一.configparser 简介Python用于读写ini文件的一个官方标准库.具体详见官网链接 二.configparser 部分方法介绍 方法 描述 read(filenames) filesn ...

  4. python(60):configparser 函数,配置文件

    ConfigParser模块学习 介绍 ConfigParser模块在python中是用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section),每 ...

  5. Python使用ConfigParser模块读取配置文件(config.ini)以及写入配置文件

    前言 使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是configParser.configPars ...

  6. Python+selenium之读取配置文件内容

    Python+selenium之读取配置文件内容 Python支持很多配置文件的读写,此例子中介绍一种配置文件的读取数据,叫ini文件,python中有一个类ConfigParser支持读ini文件. ...

  7. python ConfigParser、shutil、subprocess、ElementTree模块简解

    ConfigParser 模块 一.ConfigParser简介ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类 ...

  8. Python中操作ini配置文件

    这篇博客我主要想总结一下python中的ini文件的使用,最近在写python操作mysql数据库,那么作为测试人员测试的环境包括(测试环境,UAT环境,生产环境)每次需要连接数据库的ip,端口,都会 ...

  9. Python文件读写(open(),close(),with open() as f...)

    Python内置了读写文件的函数,用法和C是兼容的.本节介绍内容大致有:文件的打开/关闭.文件对象.文件的读写等. 本章节仅示例介绍 TXT 类型文档的读写,也就是最基础的文件读写,也需要注意编码问题 ...

随机推荐

  1. 【第二十五章】 springboot + hystrixdashboard

    注意: hystrix基本使用:第十九章 springboot + hystrix(1) hystrix计数原理:附6 hystrix metrics and monitor 一.hystrixdas ...

  2. json.dump()和json.dmups()的区别

    在python中支持json合适的数据是通过json模块实现的. 在序列化json数据的时候遇到两个形状很像的函数,dump()和dumps().主要说说他们的区别 先看看官方文档的说明:https: ...

  3. 【Python】【fmt】

      [练习]    #练习1:format print(format(3.44444,'.3e')) #3.444e+00 #练习2:findall() & finditer()import ...

  4. React Native的SliderIOS滑块组件

    import React,{Component}from 'react'; import { AppRegistry, StyleSheet, Text, View, SliderIOS, } fro ...

  5. Js页面自动跳转

    //声明 t = 1 var t = 10; function openwin() { t -= 1; if(t==0){ location.href='index2.html'; } setTime ...

  6. [设计模式]工厂模式factory

    参考:http://wxg6203.iteye.com/blog/740229 简单工厂模式(simple factory)是类的创建模式,又叫静态工厂方法(static factory method ...

  7. Spring AMQP 源码分析 04 - MessageListener

    ### 准备 ## 目标 了解 Spring AMQP 如何实现异步消息投递(推模式) ## 前置知识 <RabbitMQ入门_05_多线程消费同一队列> ## 相关资源 Quick To ...

  8. HTML基础知识(w3school)

    http://www.w3school.com.cn/tags/tag_meta.asp

  9. 基础最短路(模板 spfa)

    Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还 ...

  10. Oracle性能诊断艺术-读书笔记

    create table test0605 as select * from dba_objects; select t1.owner,t1.object_name,t1.object_id from ...