一. configparser模块

生成文档
import configparser
config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '',
'Compression': 'yes',
'CompressionLevel': '',
'ForwardX11':'yes'
}
config['bitbucket.org'] = {'User':'hg'}
config['topsecret.server.com'] = {'Host Port':'','ForwardX11':'no'}
with open('example.ini', 'w') as configfile:
config.write(configfile) 查找文件
import configparser
config = configparser.ConfigParser()
#---------------------------查找文件内容,基于字典的形式
print(config.sections()) # []
config.read('example.ini')
print(config.sections()) # ['bitbucket.org', 'topsecret.server.com']
print('bytebong.com' in config) # False
print('bitbucket.org' in config) # True
print(config['bitbucket.org']["user"]) # hg
print(config['DEFAULT']['Compression']) #yes
print(config['topsecret.server.com']['ForwardX11']) #no
print(config['bitbucket.org']) #<Section: bitbucket.org>
for key in config['bitbucket.org']: # 注意,有default会默认default的键
print(key)
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 增删改
import configparser
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','')
config.set('yuan','k2','')
f = open('new2.ini', "w")
config.write(f) # 写进文件
f.close()

二. logging模块

# 什么叫日志?
# 日志:是用来记录用户行为 或者 代码的执行过程 # logging
# 我能够‘一键’控制
# 排错的时候需要打印很多细节来帮助我排错
# 严重的错误记录下来
# 有一些用户行为 有没有错我都要记录下来
import logging
logging.debug('debug message') # 低级别的:排错信息
logging.info('info message') # 正常信息
logging.warning('warning message') # 警告信息
logging.error('error message') # 错误信息
logging.critical('critical message') # 高级别的:严重错误信息

logger对象配置

import logging

logger = logging.getLogger()
# 创建一个handler,用于写入日志文件
fh = logging.FileHandler('test.log',encoding='utf-8') # 再创建一个handler,用于输出到控制台
ch = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setLevel(logging.DEBUG) fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh) #logger对象可以添加多个fh和ch对象
logger.addHandler(ch) logger.debug('logger debug message')
logger.info('logger info message')
logger.warning('logger warning message')
logger.error('logger error message')
logger.critical('logger critical message')

python学习之老男孩python全栈第九期_day029知识点总结——configparser模快、logging模块的更多相关文章

  1. python学习之老男孩python全栈第九期_day027知识点总结——反射、类的内置方法

    一. 反射 ''' # isinstance class A:pass class B(A):pass a = A() print(isinstance(a,A)) # 判断对象和类的关系 print ...

  2. python学习之老男孩python全栈第九期_day023知识点总结——类和对象命名空间、组合

    一. 类和对象命名空间类里 可以定义两种属性: 1. 静态属性 2. 动态属性 class Course: language = 'Chinese' def __init__(self, teache ...

  3. python学习之老男孩python全栈第九期_day019知识点总结——collections模块、时间模块、random模块、os模块、sys模块

    一. collections模块 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:namedtuple.deque.Counte ...

  4. python学习之老男孩python全栈第九期_day017知识点总结——初识递归、算法

    一. 递归函数 如果一个函数在内部调用自身本身,这个函数就是递归函数. 最大递归深度默认是997 -- python从内存角度出发做得限制(而不是程序真的报错),最大深度可以修改 def func(n ...

  5. python学习之老男孩python全栈第九期_day010知识点总结

    def qqxing(l = []): # 可变数据类型 l.append(1) print(l)qqxing() # [1]qqxing([]) # [1]qqxing() # [1, 1]qqxi ...

  6. python学习之老男孩python全栈第九期_day007知识点总结

    基础数据类型汇总 1. str 2. int 3. list 4. bool 5. dict (1) fromkeys Python 字典 fromkeys() 方法用于创建一个新的字典,并以可迭代对 ...

  7. python学习之老男孩python全栈第九期_day001知识点总结

    1. Python2与Python3的区别: Python2:源码不标准,混乱,重复代码太多: Python3:统一标准,去除重复代码. 编码方式: python2的默认编码方式为ASCII码:pyt ...

  8. python学习之老男孩python全栈第九期_day014知识点总结

    # 迭代器和生成器# 迭代器 # 双下方法:很少直接调用的方法,一般情况下,是通过其他语法触发的# 可迭代的 --> 可迭代协议:含有__iter__的方法( '__iter__' in dir ...

  9. python学习之老男孩python全栈第九期_day016知识点总结

    '''数据类型:intbool... 数据结构:dict (python独有的)listtuple (pytho独有的)setstr''' # reverse() 反转l = [1,2,3,4,5]l ...

随机推荐

  1. Stack-682. Baseball Game

    You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 ...

  2. 前端入门html(表单)

    day48 配置Django项目:https://blog.csdn.net/zV3e189oS5c0tSknrBCL/article/details/79606994 <!DOCTYPE ht ...

  3. 刚才在windows下发现拖拽不了文件了

    百度了下  摁了两下esc就可以了.以下是百度得到的答案 按几下Esc目的是:没按前ESC键[接触不好]或[键不灵]或其他原因导致ESC处于[按下]状态,这样鼠标就会拖不了文件,点击菜单也会马上消失, ...

  4. Redis查询&JDBC查询&Hibernate查询方式的效率比较...

    比较三种查询方式查询效率对比...我是用的JavaWeb的方式通过通过JSP页面查询的填写查询的参数...给予反馈.... 整个demo的下载地址:http://files.cnblogs.com/f ...

  5. Fiddler4抓包工具使用教程一

    本文参考自http://blog.csdn.net/ohmygirl/article/details/17846199,纯属读书笔记,加深记忆 1.抓包工具有很多,为什么要使用Fiddler呢?原因如 ...

  6. 编译Qt-mingw使用的opencv

    set path=D:\dev\IDE\Qt5.7.0\Tools\mingw530_32\bin;%path% cd build_mingw530_32 cmake -G "MinGW M ...

  7. Spring Security构建Rest服务-1001-spring social开发第三方登录之spring social基本原理

    OAuth协议是一个授权协议,目的是让用户在不将服务提供商的用户名密码交给第三方应用的条件下,让第三方应用可以有权限访问用户存在服务提供商上的资源. 接着上一篇说的,在第三方应用获取到用户资源后,如果 ...

  8. Linux 系统计算文件夹下文件数量数目

    查看某目录下文件的个数(未包括子目录) ls -l |grep "^-"|wc -l 或 find ./company -type f | wc -l 查看某目录下文件的个数,包括 ...

  9. linux 下screen 使用

    screen命令的常规用法: screen -d -r:连接一个screen进程,如果该进程是attached,就先踢掉远端用户再连接. screen -D -r:连接一个screen进程,如果该进程 ...

  10. Linux vim 编辑命令

    vi命令命令模式:yy:复制 光标所在的这一行 4yy:复制 光标所在行开始向下的4行p: 粘贴dd:剪切 光标所在的这一行2dd:剪切 光标所在行 向下 2行D:从当前的光标开始剪切,一直到行末d0 ...