python常用模块之configparser模块

一、configparser模块的作用

configparser适用于生成并操作如下格式的配置文件

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes

[bitbucket.org]
User = hg

[topsecret.server.com]
Port = 50022
ForwardX11 = no

二、如何用configparser模块生成如上格式的配置文件

导入configparser模块后,生成一个configparser的对象,然后像字典的方式定义配置文件的内容,最后打开一个文件将定义的内容写入文件即可,比如

import configparser
config=configparser.ConfigParser()  #生成对象
config["DEFAULT"]={
                    ‘ServerAliveInterval‘: ‘45‘,
                    ‘Compression‘: ‘yes‘,
                    ‘CompressionLevel‘: ‘9‘,
                    ‘ForwardX11‘:‘yes‘
}
config["bitbucket.org"]={‘User‘:‘hg‘}  #生成配置文件的内容
config[‘topsecret.server.com‘]={‘Host Port‘:‘50022‘,‘ForwardX11‘:‘no‘}
with open(‘example.ini‘,‘w‘) as configfile:
    config.write(configfile)  #将配置写入文件

三、从配置文件中获取信息

import configparser
config=configparser.ConfigParser()
config.read(‘example.ini‘) #读入配置文件
print(config.sections())  #输出所有节点名称
print(‘bytebong.com‘ in config)
print(‘bitbucket.org‘ in config)  #判断指定节点是否存在
print(config[‘bitbucket.org‘]["user"])  #取值
print(config[‘bitbucket.org‘])  #输出节点名称
for key in config[‘bitbucket.org‘]: #输出节点中每个配置项的名字,如果有DEFAULT则会将DEFAULT的配置项也一起输出
    print(key)
print(config.options(‘bitbucket.org‘))#作用同上面的for,结果为列表
print(config.items(‘bitbucket.org‘))#输出为列表,元素是每个配置项何其参数的元祖,同样会输出DEFAULT的配置
print(config.get(‘bitbucket.org‘,‘user‘))#获取指定节点中指定配置项的参数,同样可以获取DEFAULT中的配置项参数

四、增删改

import configparser

config = configparser.ConfigParser()

config.read(‘example.ini‘)

config.add_section(‘yuan‘) #创建新节点

config.remove_section(‘bitbucket.org‘) #删除节点

config.remove_option(‘topsecret.server.com‘,"forwardx11") #删除配置项

config.set(‘topsecret.server.com‘,‘k1‘,‘11111‘) #增加配置项

config.set(‘yuan‘,‘k2‘,‘22222‘)

config.write(open(‘new2.ini‘, "w")) #将修改后的配置写入文件

时间: 2024-08-25 11:21:11

python常用模块之configparser模块的相关文章

python基础13 ---函数模块4(configparser模块)

configparser模块 一.configparser模块 1.什么是configparser模块:configparser模块操作配置文件,配置文件的格式与windows ini和linux的cf文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值),其配置文件(INI文件)由节(section).键.值组成. 2.configparser模块简介. ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号"[ ]"内包含的为sect

python学习-shutil,configparser模块

shutil模块 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作.对单个文件的操作也可参见os模块. shutil.copyfileobj(fsrc, fdst[, length]) shutil.copyfileobj(fsrc, fdst[, length]):复制文件内容(不包含元数据)从类文件对象src到类文件对dst.可选参数length指定缓冲区的大小,负数表示一次性读入.默认会把数据切分成小块拷贝,以免占用太多内存.注意:拷

Python常用的内建模块

PS:Python之所以自称“batteries included”,就是因为内置了许多非常有用的模块,无需额外安装和配置,即可直接使用.下面就来看看一些常用的内建模块. datetime dateime是Python中处理日期和时间的标准库. 获取当前日期和时间 原文地址:https://www.cnblogs.com/yunche/p/8999052.html

Python之路(第十八篇)shutil 模块、zipfile模块、configparser模块

一.shutil 模块 1.shutil.copyfileobj(fsrc, fdst[, length]) 将文件内容拷贝到另一个文件中,需要打开文件 import shutil shutil.copyfileobj(open("old_test.txt","r"),open("new_test.txt","w")) 输出结果 2.shutil.copyfile(src,dst) 复制文件内容到另外一个文件,不需要打开文件,

【Python模块】configparser模块

configparser模块: 是python标准库用来解析配置文件的模块. 格式: section:使用[]标记section名 :或= :使用:或=赋值 [websv] ip:'192.168.1.10' port:443 name = 'root' pw = 'root1990' 定义: websv叫section 同一个项可以多个值: ip:'192.168.1.11','192.168.1.12','192.168.1.13'  #待测试 read配置文件时,会自动把参数名变成小写 一

python读取配置文件 变量 ConfigParser模块

Python 读取写入配置文件很方便,可使用内置的 configparser 模块配置文件:config.ini [oppo] platformName = Android platformVersion = 6.0 deviceName = weiruoyu appPackage = com.sina.weibo appActivity = .SplashActivity url = http://127.0.0.1:4723/wd/hub [mysql] host=127.0.0.1 por

python模块之configparser模块

configparser模块:用于按一定格式创建配置文件 创建 import configparser config = configparser.ConfigParser() config['DEFAULT'] = {'default': 'yes'} config['path'] = {'userinfo': r'E:\pycharm\学习\day29\userinfo'} with open('userinfo.ini', 'w', encoding='utf-8') as f: conf

os模块、os.path模块、shutil模块、configparser模块、subprocess模块

一.os模块 os指的是错作系统 该模块主要用于处理与操作系统相关的操作,常用的是文件操作(读.写.删.复制.重命名). os.getcwd()  获取当前文件所在的文件夹路径 os.chdir()  修改当前工作目录 os.makedirs()  创建多级目录(不存在时,则创建) os.mkdir()  创建单级目录 os.rmdir()  删除文件夹(文件夹为空,则删除) os.listdir()  列出指定目录下的所有文件及子目录(存在一个列表中) os.stat()  获取文件信息 os

处理xml模块、configparser模块、hashlib模块、subprocess模块

xml模块 新建a.xml内容为: <data> <country name="Liechtenstein"> <rank updated="yes">2</rank> <year updated="yes" version="1.0">2009</year> <gdppc>141100</gdppc> <neighbor d