config读取操作
cf = configparser.ConfigParser()    # 实例化对象
cf.read(filename)    # 读取文件
cf.sections()    # 读取sections值, 返回一个list
cf.options(sections)    # 读取options值, 返回一个list
cf.items(sections)    # 读取指定sections下所有的键值对, 返回list
cf.get(sections, key)    # 获取指定sections下指定key的value值, 返回str
cf.getint(sections, key)    # 获取指定sections下指定key的value值, 返回int
# encoding: utf-8

import configparser
import os class ReadConifg():
"""读取配置文件"""
def __init__(self, filename, filepath):
os.chdir(filepath) # 将当前工作目录切换到指定的目录
self.cf = configparser.ConfigParser() # 实例化configparser对象
self.cf.read(filename) # 读取文件 def read_sections(self):
"""读取配置文件中sections"""
sacs = self.cf.sections() # 获取sections,返回list
print("sections:", sacs, type(sacs)) def read_options(self, sections):
"""获取配置文件中的options值"""
opts = self.cf.options(sections) # 获取db section下的options,返回list
print("%s:%s" % (sections, opts), type(opts)) def read_kv(self, sections):
"""获取配置文件中的所有键值对"""
kvs = self.cf.items(sections) # 获取db section下的所有键值对,返回list
print("%s:%s" % (sections, kvs)) def get_str(self, sections, key):
"""获取指定sectons中指定的key的value值, 返回的会是 str 类型"""
value_str = self.cf.get(sections, key)
return value_str def get_int(self, sections, key):
"""获取指定sectons中指定的key的value值, 返回的会是 int 类型"""
value_int = self.cf.getint(sections, key)
return value_int

读取config文件

config写入操作

cf = configparser.ConfigParser()    # 实例化对象
cf.read(self.filename)    # 读取文件,如果是重新写入,覆盖原有内容不需要读取 
cf.add_section(section)    # 添加sections值
cf.set(section, option, value)    # 在指定的sections中添加键值对
cf.remove_section(section)    # 移除sections, 需要先cf.read(filename)
cf.remove_option(section, option)    # 移除指定sections下的options, 需要先cf.read(filename)
class WriteConfig():
"""写入config文件"""
def __init__(self, filename, filepath=r"D:\python_file\boke\config"):
self.filename = filename
os.chdir(filepath)
self.cf = configparser.ConfigParser()
self.cf.read(self.filename) # 如果修改,则必须读原文件 def _with_file(self):
# write to file
with open(self.filename, "w+") as f:
self.cf.write(f) def add_section(self, section):
# 写入section值
self.cf.add_section(section)
self._with_file() def set_options(self,section, option, value=None):
"""写入option值"""
self.cf.set(section, option, value)
self._with_file() def remove_section(self, section):
"""移除section值"""
self.cf.remove_section(section)
self._with_file() def remove_option(self, section, option):
"""移除option值"""
self.cf.remove_option(section, option)
self._with_file()

写入config文件

												

python向config、ini文件读取写入的更多相关文章

  1. INI文件的写入与读取

    INI文件的写入与读取 [节名]         '[]中的节名对应此API的第一参数 Name=内容      'Nmae对应此API的第二参数 API的第三参数是没有取到匹配内容时返回的字符串; ...

  2. 轮子:读取config.ini文件

    python: 把config.ini文件成map返回 def get_conf(conf_file): conf = {} ll=list(map(lambda x: x.replace('&quo ...

  3. C#对config.ini文件进行读取和修改

    C#对config.ini文件进行读取和修改: public partial class Patrolcar : Form之后可以加入如下类: #region public class IniFile ...

  4. PostgreSql那点事(文件读取写入、命令执行的办法)

    • 2013/07/9 作者: admin PostgreSql那点事(文件读取写入.命令执行的办法) 今天无意发现了个PostgreSQL环境,线上学习了下,一般的数据注射(读写数据库)差异不大,不 ...

  5. 1. 在config.ini文件中加入dm.park.time=1,会使uap中的tomcat启动加快

    在config.ini文件中加入dm.park.time=1,会使uap中的tomcat启动加快

  6. python中readline判断文件读取结束的方法

    注:内容来自网络 本文实例讲述了python中readline判断文件读取结束的方法.分享给大家供大家参考.具体分析如下: 大家知道,python中按行读取文件可以使用readline函数,下面现介绍 ...

  7. 91.生成ini文件并写入和读取ini文件

    写入 WritePrivateProfileStringA("hello money", infx[i].name, money, "1.ini"); 按照字符 ...

  8. C# 创建INI文件,写入并可读取。----转载

    基于C#winform设计. 首先创建一个类,我命名为IniFiles.并引入命名空间using System.Runtime.InteropServices; 接着,声明API函数 [DllImpo ...

  9. 【代码】ini 文件读取工具类

    using System; using System.Runtime.InteropServices; using System.Text; namespace hrattendance.Common ...

随机推荐

  1. svg常见形状

    SVG是使用XML来描述二维图形和绘图程序的语言.是指可伸缩矢量图形(Scalable Vector Graphics),svg.图像在放大或改变尺寸的情况下图形质量不会有所损失. svg的主要竞争者 ...

  2. 实验三 CC2530平台上CC2530平台上定时器组件的

    实验三 CC2530平台上CC2530平台上定时器组件的TinyOS编程 实验目的: 加深和巩固学生对于TinyOS编程方法的理解和掌握 让学生初步掌握CC2530定时器的PWM功能,及其TinyOS ...

  3. 为什么要用docker

    一:更高效的利用系统资源            由于容器不需要进行硬件虚拟以及运行完整操作系统等额外开销,Docker 对系统资源的利用率更高.无论是应用执行速度.内存损耗或者文件存储速度,都要比传统 ...

  4. 13. Forensics (取证 4个)

    Maltego是一个取证和数据挖掘应用程序. 它能够查询各种公共数据源,并以图形方式描绘人员,公司,网站和文档等实体之间的关系. Maltego也是一个开源智能,但不是开源软件. Helix The ...

  5. C语言学习笔记之位运算求余

    我们都知道,求一个数被另一个数整除的余数,可以用求余运算符”%“,但是,如果不允许使用求余运算符,又该怎么办呢?下面介绍一种方法,是通过位运算来求余,但是注意:该方法只对除数是2的N次方幂时才有效. ...

  6. C# EnumHelper Enum的值,Description,ToString()的相互转换

    首先定义枚举类型,如下: /// <summary> /// 板块 /// </summary> public enum Plate {         [Descriptio ...

  7. python统计目录和目录下的文件,并写入excel表

    运营那边提出需求,有些媒体文件需要统计下 目录结构大概是这样的 每个目录下面都有很多文件,目录下面没子目录 我这里是模拟下创建的目录和文件,和运营那边说的目录结构都是一致的 想最终统计结果如下格式 我 ...

  8. python-简单的登陆接口

    # 工作需求2 编写登陆接口(1输入用户名密码 2认证成功后显示欢迎信息 3输错三次后锁定)# 实现思路 1.用户名和密码是否正确,全部正确可正常登陆 输错三次无法登陆 2.是否已经注册 未注册 无法 ...

  9. 关闭防火墙,selinux,交互式设置IP的脚本

    脚本内容: #!/bin/bash # ens=$(cat /proc/net/dev | awk '{if($2>0 && NR > 2) print substr($1 ...

  10. RK3399/NanoPC-T4开发板低级格式化SD卡,恢复SD卡和TF卡 方法

    恢复SD卡和TF卡 方法:Windows下运行:HDDLLF.4.40.exe执行Low-Level format然后拔插,重新插在win10下提示格式化,则执行格式化操作,即可恢复. 应用场合:使用 ...