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. react抛物线小球实现

    .ballFather{ position: absolute; width: 15px; height: 15px; border-radius: 50%; transition: all 0.5s ...

  2. JavaWeb(二)

    三.搭建JavaWeb应用开发环境——Tomcat服务器 3.1.疑问:学习web开发,为什么必须要先装一个WEB服务器? 在本地计算机上随便创建一个web页面,用户是无法访问到的,但是如果启动tom ...

  3. Sublime3安装及配置

    1.官网下载 2.安装后输入密钥 ----- BEGIN LICENSE -----sgbteamSingle User LicenseEA7E-11532598891CBB9 F1513E4F 1A ...

  4. c#实现识别图片上的验证码数字

    这篇文章主要介绍了c#实现识别图片上的验证码数字的方法,本文给大家汇总了2种方法,有需要的小伙伴可以参考下. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 ...

  5. 网络请求 Requests

    网络请求 Requests url: 就是需要请求,并进行下一步处理的urlcallback: 指定该请求返回的Response,由那个函数来处理.method: 一般不需要指定,使用默认GET方法请 ...

  6. 面试题-linux基础

    Linux基础和git linux的基础命令(怎么区分一个文件还是文件夹) ls -F  在显示名称的时候会在文件夹后面添加“/”, 在文件后面加“*” 日志以什么格式,存放在哪里?日志可以存储在“/ ...

  7. char* 与 string 互转

    因为c#强调安全性,每次意图将string的地址赋给指针时,系统都要报错,原因是系统无法计算字符串的空间和地址,这里不多bb,使用IntPtr类(using Runtime.InteropServic ...

  8. 当yum安装出现Error: Package: glibc-headers .....时

    环境 CentOS Linux release 7.4.1708 (Core)   当使用yum源安装时,出现以下报错 Error: Package: glibc-headers-.el7_4..x8 ...

  9. MGR实现分析 - 成员管理与故障恢复实现

    MySQL Group Replication(MGR)框架让MySQL具备了自动主从切换和故障恢复能力,举single primary(单主)模式为例,primary作为主节点对外提供读写服务,是唯 ...

  10. Spring Boot 全局异常处理

    Spring Boot版本 1.5 @ControllerAdvice public class GlobalExceptionHandler extends ResponseEntityExcept ...