Python configparser模块操作代码实例
作者:wenbin_ouyang 发布时间:2021-11-05 18:48:08
标签:Python,configparser,模块
1、生成配置文件
'''
生成配置文件
'''
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
来源:https://www.cnblogs.com/xy-ouyang/p/12986386.html


猜你喜欢
- 本文实例讲述了Python实现列表转换成字典数据结构的方法。分享给大家供大家参考,具体如下:'''[ {
- 统计在线人数是实时的吗?实现起来也比较简单,见下列代码:global.asa<SCRIPT LANGUAGE="V
- 三角函数如果我们以OP作为圆的半径r,以o点作为圆的圆心,圆上的点的x坐标就是r * cos a ,y坐标就是 r * sin a。pyth
- 递归和尾递归简单的说,递归就是函数自己调用自己,它做为一种算法在程序设计语言中广泛应用。其核心思想是把一个大型复杂的问题层层转化为一个与原问
- 正则表达式用于字符串处理、表单验证等场合,实用高效。现将一些常用的表达式收集于此,以备不时之需。文章转载自:http://www.phpch
- 一种有意思的数据结构-默克树(Merkle tree)默克树(Merkle tree)又叫hash树。程序员可以说自己不知道默克树,但是不能
- 首先选择操作系统。由于ASP属于MS(Microsoft)的东西,所以我们要选择MS的操作系统,Windows 98以上就可以(
- WARNING:低技术力自己无聊写的哥特字体是最好看的:示例代码:#!usr/bin/env python3# -*- coding:UTF
- 在Perfection kills上看到他去年写的一篇文章,关于HTML优化的,讲的很详细,姑且记录之,尽管里面有些东西并不能在目前的环境里
- 1. datetime 库概述以不同格式显示日期和时间是程序中最常用到的功能。Python 提供了一个处理时间的标准函数库 datetime
- Python 网页解析HTMLParse的实例详解使用python将网页抓取下来之后,下一步我们就应该解析网页,提取我们所需要的内容了,在p
- 本文为大家分享了MySQL 8.0.29 安装配置方法图文教程,供大家参考,具体内容如下一、下载MySQL1、进入MySQL官网MySQL并
- 前言前面介绍了Allure报告,本篇来学习普通的HTML如何展示在Jenkins上安装插件Manage Jenkins --> Man
- Oracle游标分为显示游标和隐式游标。显示游标(Explicit Cursor):在PL/SQL程序中定义的、用于查询的游标称作显示游标。
- 本文实例分析了php中Ctype函数用法。分享给大家供大家参考。具体分析如下:Ctype函数是Php的Ctype扩展函数提供了一组函数用于校
- 1、django搜索路径使用 import 语句时,Python 所查找的系统目录清单。查看方式:import sysprint
- 本文实例讲述了php简单定时执行任务的实现方法。分享给大家供大家参考。具体实现方法如下:<?phpignore_user_abort(
- 本文实例讲述了Python学习笔记之lambda表达式用法。分享给大家供大家参考,具体如下:Lambda 表达式使用 Lambda 表达式创
- 1、将css与javascript全部用下边的方法分离到外部文件中去。<link rel="stylesheet"
- 概述:each() 方法规定为每个匹配元素规定运行的函数。返回 false 可用于及早停止循环,相当于break。返回 true 可以结束本