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:
    config.write(f)

  完成后的文件:

[DEFAULT]
default = yes

[path]
userinfo = E:\pycharm\学习\day29\userinfo

  查看

print(config.sections())  # []        因为还没有读取文件
config.read(‘userinfo.ini‘, encoding=‘utf-8‘)
print(config.sections())  # [‘path‘]  读出节点  [‘DEFAULT‘]为默认节点 不打印
print(‘path‘ in config)   # True      判断某节点是否在配置文件中
print(config[‘path‘][‘userinfo‘])  # E:\pycharm\学习\day29\userinfo   读取节点下的配置项  没有该项目标报错
print(config.get(‘path‘, ‘userinfo‘))  # E:\pycharm\学习\day29\userinfo

for k in config[‘path‘]:   # 打印‘path‘节点下的配置项的同时还会打印默认节点下的所有项
    print(k)  # userinfo  default

print(config.items(‘path‘))  # [(‘default‘, ‘yes‘), (‘userinfo‘, ‘E:\\pycharm\\学习\\day29\\userinfo‘)]

  增加

config.read(‘userinfo.ini‘, encoding=‘utf-8‘)
config.add_section(‘IP‘)  # 增加节点
print(config.sections())  # [‘path‘, ‘IP‘]
config.set(‘IP‘, ‘ip‘, ‘192.168.1.1‘)  # 给节点增加配置项
config.set(‘path‘, ‘userinfo‘, ‘None‘)  # 修改配置项
print(config[‘IP‘][‘ip‘])  # 192.168.1.1
print(config[‘path‘][‘userinfo‘])  # None
config.write(open(‘userinfo.ini‘, ‘w‘, encoding=‘utf-8‘))  # 将修改重新写回文件

  删除

config.read(‘userinfo.ini‘, encoding=‘utf-8‘)
print(config.sections())  # [‘path‘, ‘IP‘]
config.remove_section(‘IP‘)  # 删除节点
print(config.sections())  # [‘path‘]
print(config.items(‘path‘))  # [(‘default‘, ‘yes‘), (‘userinfo‘, ‘None‘)]
config.remove_option(‘path‘, ‘userinfo‘)  # 删除节点中的配置项
print(config.items(‘path‘))  # [(‘default‘, ‘yes‘)]
config.write(open(‘userinfo.ini‘, ‘w‘, encoding=‘utf-8‘))  # 将修改重新写回文件

原文地址:https://www.cnblogs.com/zxc-Weblog/p/8342649.html

时间: 2024-08-29 18:01:14

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

Python 标准库 ConfigParser 模块 的使用

Python 标准库 ConfigParser 模块 的使用 demo #!/usr/bin/env python # coding=utf-8 import ConfigParser import sys config = ConfigParser.ConfigParser() #写入 config.add_section("Inc_basic") config.set("Inc_basic","name","iPIN")

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

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

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模块读写配置文件

之前的博客介绍过利用python的configparser模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高了代码的可读性... 1.配置文件 configparser模块支持读取.conf和.ini等类型的文件,在文件夹新建一个.ini文件,写入一些信息,示例如下: config.ini [driver] chromedriver = E:\automation\UI\Testcase\browser\

【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模块

在实际的开发过程中,我们常有操作ini格式和conf格式配置文件的操作,Python为我们提供了configparser模块,方便我们对配置文件进行读写操作. config.ini配置文件内容如下: [email] user = root password = aaaaaa port = 25 host = smtp.126.com [thread] thread = 3 1.读取配置文件 方法 说明 read(filename) 直接读取配置文件内容 sections() 以列表的形式返回所有

python解析配置文件---configparser模块

1.configparser模块介绍 configparser是用来读取配置文件的模块,配置文件格式为:中括号"[ ]"内包含的为section.section 下面为类似于key-value 的配置内容. a.conf的文件内容如下: [user01] name = user01 is_admin = True age = 34 passwd = user123456 [yxwang] name = yxwang age = 25 passwd = 123456 取值: import

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模块生成如上格式的配置文件

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

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