1、生成配置文件

'''
生成配置文件

很多人学习python,不知道从何学起。
很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手。
很多已经做案例的人,却不知道如何去学习更加高深的知识。
那么针对这三类人,我给大家提供一个好的学习平台,免费领取视频教程,电子书籍,以及课程的源代码!
QQ群:1097524789

'''
import configparser config = configparser.ConfigParser() # 初始化赋值
config["DEFAULT"] = {'ServerAliveInterval': '45',
'Compression': 'yes',
'CompressionLevel': '9'}
# 追加
config['DEFAULT']['ForwardX11'] = 'yes' config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg' config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022' # mutates the parser
topsecret['ForwardX11'] = 'no' # same here with open('example.ini', 'w') as configfile:
config.write(configfile)

2、读取配置文件

# 读
import configparser
config = configparser.ConfigParser()
config.sections()
config.read('example.ini')
# {'serveraliveinterval': '45', 'compression': 'yes', 'compressionlevel': '9', 'forwardx11': 'yes'}
print(config.defaults()) # hg
print(config['bitbucket.org']["User"]) # 50022
print(config["topsecret.server.com"]["host port"])

3、删除

# 删除(创建一个新文件,并删除 bitbucket.org)
import configparser
config = configparser.ConfigParser()
config.sections() config.read('example.ini')
rec = config.remove_section("bitbucket.org") # 删除该项
config.write(open("example.cfg","w"))

生成新文件 example.cfg

DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes topsecret.server.com]
host port = 50022
forwardx11 = no

删除,并覆盖原文件

# 删除(删除 bitbucket.org)
import configparser
config = configparser.ConfigParser()
config.sections() config.read('example.ini')
rec = config.remove_section("bitbucket.org") # 删除该项
config.write(open("example.ini","w"))

4、修改

import configparser

config = configparser.ConfigParser()

config.read('example.ini') #读文件

config.add_section('yuan') #添加section

config.remove_section('bitbucket.org') #删除section
config.remove_option('topsecret.server.com',"forwardx11") #删除一个配置项 config.set('topsecret.server.com','k1','11111')
config.set('yuan','k2','22222')
with open('new2.ini','w') as f:
config.write(f)

生成新文件 new2.ini

[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes [topsecret.server.com]
host port = 50022
k1 = 11111 [yuan]
k2 = 22222

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们

Python configparser模块操作代码实例的更多相关文章

  1. python自动化--模块操作之re、MySQL、Excel

    一.python自有模块正则 import re # re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None print(re.match("www ...

  2. Python中类的继承代码实例

    Python中类的继承代码实例 这篇文章主要介绍了Python中类的继承代码实例,本文直接给出代码及运行效果,需要的朋友可以参考下 相对于C 的继承编写,Python更简洁,而且效率也是很高的,下面编 ...

  3. python -ConfigParser模块讲解

    configParser 模块用于操作配置文件 注:Parser汉译为“解析”之意. 配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键= ...

  4. Python - configParser模块学习

    configParser 模块用于操作配置文件 注:Parser汉译为“解析”之意. 配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键= ...

  5. python configparser模块详解

    此模块提供了一个实现基本配置语言的类 首先来看一个非常基本的配置文件,如下所示格式: [DEFAULT] ServerAliveInterval = 45 Compression = yes Comp ...

  6. Python Configparser模块读取、写入配置文件

    写代码中需要用到读取配置,最近在写python,记录一下. 如下,假设有这样的配置. [db] db_host=127.0.0.1 db_port=3306 db_user=root db_pass= ...

  7. 【python】python configparser模块

    ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section), 每个节可以有多个参数(键=值).使用的配置 ...

  8. python ConfigParser模块 配置文件解析

    ConfigParser模块主要是用来解析配置文件的模块,像mysql,或者win下面的ini文件等等 下面我们来解析mysql的配置文件my.cnf my.cnf配置文件内容 [mysqld] da ...

  9. python ConfigParser 模块

    ConfigParser的函数方法 读取配置文件 read(filename) 直接读取ini文件内容 sections() 得到所有的section,并以列表的形式返回 options(sectio ...

随机推荐

  1. Reface.AppStarter 类型扫描 —— 获得系统中所有的实体类型

    类型扫描 是 Reface.AppStarter 提供的最基本.最核心的功能. AutoConfig , ComponentScan 等功能都是基于该功能完成的. 每一个使用 Reface.AppSt ...

  2. bzoj4236JOIOJI

    bzoj4236JOIOJI 题意: 给一个只由JOI三个字母组成的串,求最长的一个子串使其中JOI三个字母出现次数相等.串长度≤200000 题解: 有点像bzoj4384,因此推算的过程是差不多的 ...

  3. Spring Boot Redis 实现分布式锁,真香!!

    之前看很多人手写分布式锁,其实 Spring Boot 现在已经做的足够好了,开箱即用,支持主流的 Redis.Zookeeper 中间件,另外还支持 JDBC. 本篇栈长以 Redis 为例(这也是 ...

  4. Java常用API(String类)

    Java常用API(String类) 概述: java.lang.String 类代表字符串.Java程序中所有的字符串文字(例如 "abc" )都可以被看作是实现此类的实例 1. ...

  5. 树形dp 之 小胖守皇宫

    题目描述 huyichen世子事件后,xuzhenyi成了皇上特聘的御前一品侍卫. 皇宫以午门为起点,直到后宫嫔妃们的寝宫,呈一棵树的形状:有边相连的宫殿间可以互相望见.大内保卫森严,三步一岗,五步一 ...

  6. NVIDIA GPU Volta架构简述

    NVIDIA GPU Volta架构简述 本文摘抄自英伟达Volta架构官方白皮书:https://www.nvidia.com/content/dam/en-zz/Solutions/Data-Ce ...

  7. vue学习(十一) v-for使用的注意事项:2.2.0+之后的版本里,当在组件中使用v-for时,key是必须的,它是用来表示唯一身份的

    //html <div id="app"> <div> <label>id <input type="text" v- ...

  8. lua中 table.getn(t) 、#t、 table.maxn(t) 这三个什么区别?

    lua中 table.getn(t) .#t. table.maxn(t) 这三个什么区别? RTlocal t = {1,888,x= 999,b=2,5,nil,6,7,[10]=1,8,{z = ...

  9. Fortify Audit Workbench 笔记 Race Condition: Singleton Member Field 竞争条件:单例的成员字段

    Race Condition: Singleton Member Field 竞争条件:单例的成员字段 Abstract Servlet 成员字段可能允许一个用户查看其他用户的数据. Explanat ...

  10. MacOS IDEA下SVN配置与使用

    第一部分,准备工作 到IDEA的配置下设置SVN命令,一般来说,IDEA已经设置好了,如果没有自己找到存放SVN命令的目录,将SVN配置进去,命令应该存放在/Library/Developer/Com ...