configparser模块        # 该模块适用于配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值)。
import configparser
config = configparser.ConfigParser()
config['bitbucket.org'] = {'User':'hg'}
config['topsecret.server.com'] = {'Host Port':'50022','ForwardX11':'no'}
with open('example.ini', 'w') as configfile:config.write(configfile)
#---------------------------查找文件内容,基于字典的形式
config = configparser.ConfigParser()
config.read('example.ini')
print(config.sections())
print('bytebong.com' in config)                     # False
print(config['bitbucket.org']["user"])              # hg
for key in config['bitbucket.org']:print(key)        # 注意,有default会默认default的键
print(config.options('bitbucket.org'))              # 同for循环,找到'bitbucket.org'下所有键
print(config.items('bitbucket.org'))                # 找到'bitbucket.org'下所有键值对
print(config.get('bitbucket.org','compression'))    # yes    get方法Section下的key对应的value
#---------------------------增删改
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')
config.write(open('new2.ini', "w"))import logging
# logging.basicConfig(level=logging.WARNING,
#                     format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
#                     datefmt='%a, %d %b %Y %H:%M:%S')
# logging.debug('debug message')        # 低级别的 # 排错信息
# logging.info('info message')            # 正常信息
# logging.warning('warning message')    # 警告信息
# logging.error('error message')        # 错误信息
# logging.critical('critical message')    # 高级别的 # 严重错误信息
# # basicconfig 简单 能做的事情相对少    # 中文的乱码问题    # 不能同时往文件和屏幕上输出
#
# 配置log对象 稍微有点复杂 能做的事情相对多
import logging
logger = logging.getLogger()        #logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('log.log',encoding='utf-8')    # 创建一个文件控制对象
sh = logging.StreamHandler()                            # 创建一个屏幕控制对象
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
formatter2 = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s [line:%(lineno)d] : %(message)s')
# 文件操作符 和 格式关联
fh.setFormatter(formatter)
sh.setFormatter(formatter2)
# logger 对象 和 文件操作符 关联
logger.addHandler(fh)
logger.addHandler(sh)
logging.debug('debug message')    # 低级别的 # 排错信息
logging.info('info message')    # 正常信息
logging.warning('警告错误')        # 警告信息
logging.error('error message')    # 错误信息
logging.critical('critical message')    # 高级别的 # 严重错误信息

转载于:https://www.cnblogs.com/ming-yuan/p/9536876.html

configparser logging相关推荐

  1. 常用模块之hashlib,configparser,logging模块

    常用模块二 hashlib模块 hashlib提供了常见的摘要算法,如md5和sha1等等. 那么什么是摘要算法呢?摘要算法又称为哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定 ...

  2. 常用模块(subprocess/hashlib/configparser/logging/re)

    一.subprocess(用来执行系统命令) import oscmd = r'dir D:xxx | findstr "py"' # res = subprocess.Popen ...

  3. Linux 应用程序开发入门

    Linux 应用程序开发入门 Neo Chen (netkiller) <openunix@163.com> 版权 © 2011, 2012 http://netkiller.github ...

  4. Python全栈开发【模块】

    本节内容: 模块介绍 time random os sys json & picle shelve XML hashlib ConfigParser logging 模块介绍 在Python中 ...

  5. hashlib摘要算法模块,logging日志,configparser配置文件模块

    一.hashlib模块(摘要算法模块) 1.算法介绍 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢? 摘要算法又称哈希算法.散列算法.它通过一个函数,把 ...

  6. request,logging,ConfigParser——接口框架

    做一个将参数和用例分开放置,并且输出log的接口测试框架 我的框架如下所示 Log文件用来设置log输出文件,需要时可以在用例内调用输出,config用来填写一切需要的参数信息,jiekou_post ...

  7. hashlib\logging\configparser

    #!/usr/bin/env python # -*- coding=utf-8 -*-import hashlib# hash:哈希算法,结果是内存地址 # print(hash('123')) # ...

  8. 初学者python笔记(hashlib加密、logging日志模块、configparser配置文件)

    文章目录 logging模块 configparser模块 hashlib模块 我们在很多情况下,程序的运行都需要做日志或者某些记录,这就需要用到logging模块来自动记录:同样的,配置文件也是程序 ...

  9. 【转】模块(configparser+shutil+logging)

    [转]模块(configparser+shutil+logging) 一.configparser模块 1.模块介绍 configparser用于处理特定格式的文件,其本质上是利用open来操作文件. ...

最新文章

  1. 拼多多面试|如何用 Redis 统计独立用户访问量?
  2. 简单编程代码_好了好了,不闲扯了,我们开始学编程了
  3. Python类中的self到底是干啥的
  4. 解决无法将自己github上的代码clone到/usr目录下的问题
  5. python导包路径问题_python的导包问题
  6. 关于linux中的上下文切换
  7. HRBUST 1473 教主的遗产【状态压缩】
  8. centos安装Gitlab Runner
  9. PDF to Word OCR for Mac(PDF文档转换成word格式)
  10. Mysql复合索引的顺序和必要值
  11. sql语句基本查询日期
  12. 启动Matlab安装包时出现Unable to open the requested feature.
  13. CSS修改input标签中placeholder值的文字样式
  14. 做一个jQuery插件---带“旋转木马”效果的轮播图
  15. 深圳mba学费一览表
  16. Git如何合并分支到主干及合并主干到分支
  17. 计算机视觉方向简介 | 机器视觉检测技术
  18. thinking in uml 大象 用例
  19. 吞吐量和IOPS测试
  20. 洛谷 P1566 加等式

热门文章

  1. Mysql高可用集群-解决MMM单点故障
  2. windows下node安装
  3. 搭建一个redis高可用系统
  4. duilib入门简明教程 -- 部分bug (11) (转)
  5. typescript主键自增长
  6. OA系统部署短信过程
  7. JavaFX上手--第1天
  8. layui网页html编辑器,layui使用富文本编辑器
  9. cprintdialog预览_怎样用CPrintDialog来实现打印功能
  10. 将你的Vim 打造成轻巧强大的IDE