python学习之老男孩python全栈第九期_day029知识点总结——configparser模快、logging模块
一. 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模块的更多相关文章
- python学习之老男孩python全栈第九期_day027知识点总结——反射、类的内置方法
一. 反射 ''' # isinstance class A:pass class B(A):pass a = A() print(isinstance(a,A)) # 判断对象和类的关系 print ...
- python学习之老男孩python全栈第九期_day023知识点总结——类和对象命名空间、组合
一. 类和对象命名空间类里 可以定义两种属性: 1. 静态属性 2. 动态属性 class Course: language = 'Chinese' def __init__(self, teache ...
- python学习之老男孩python全栈第九期_day019知识点总结——collections模块、时间模块、random模块、os模块、sys模块
一. collections模块 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:namedtuple.deque.Counte ...
- python学习之老男孩python全栈第九期_day017知识点总结——初识递归、算法
一. 递归函数 如果一个函数在内部调用自身本身,这个函数就是递归函数. 最大递归深度默认是997 -- python从内存角度出发做得限制(而不是程序真的报错),最大深度可以修改 def func(n ...
- python学习之老男孩python全栈第九期_day010知识点总结
def qqxing(l = []): # 可变数据类型 l.append(1) print(l)qqxing() # [1]qqxing([]) # [1]qqxing() # [1, 1]qqxi ...
- python学习之老男孩python全栈第九期_day007知识点总结
基础数据类型汇总 1. str 2. int 3. list 4. bool 5. dict (1) fromkeys Python 字典 fromkeys() 方法用于创建一个新的字典,并以可迭代对 ...
- python学习之老男孩python全栈第九期_day001知识点总结
1. Python2与Python3的区别: Python2:源码不标准,混乱,重复代码太多: Python3:统一标准,去除重复代码. 编码方式: python2的默认编码方式为ASCII码:pyt ...
- python学习之老男孩python全栈第九期_day014知识点总结
# 迭代器和生成器# 迭代器 # 双下方法:很少直接调用的方法,一般情况下,是通过其他语法触发的# 可迭代的 --> 可迭代协议:含有__iter__的方法( '__iter__' in dir ...
- python学习之老男孩python全栈第九期_day016知识点总结
'''数据类型:intbool... 数据结构:dict (python独有的)listtuple (pytho独有的)setstr''' # reverse() 反转l = [1,2,3,4,5]l ...
随机推荐
- Memoization-329. Longest Increasing Path in a Matrix
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- Django准备知识-web应用、http协议、web框架、Django简介
一.web应用 Web应用程序是一种可以通过web访问的应用程序(web应用本质是基于socket实现的应用程序),程序的最大好处是用户很容易访问应用程序,用户只需要有浏览器即可,不需要再安装其他软件 ...
- [AIR] AIR程序调用本地默认应用程序打开本地文件
摘要: File类提供了一个方法openWithDefaultApplication可以用本地默认应用程序打开指定路径下的文件. 当我用下面语句的时候,可以成功打开桌面文件夹下面的文件: v ...
- nodejs改变代码不需要重启的方法
1.node 搭建本地服务器 在F:/node文件夹下新建app.js const http = require('http'); http.createServer((req, res) => ...
- [JS深入学习]——数组对象排序
(转) JavaScript实现多维数组.对象数组排序,其实用的就是原生的sort()方法,用于对数组的元素进行排序. sort() 方法用于对数组的元素进行排序.语法如下: arrayObject. ...
- fail2ban
在 [DEFAULT] 全局配置中的ignoreip选项中添加被放行的ip地址:ignoreip = 127.0.0.1 172.17.1.218 网段可以加 127.0.0.1/8,用空格隔开就行. ...
- jvm高级特性(2)(判断存活对象算法,finaliza(),方法区回收)
JVM高级特性与实践(二):对象存活判定算法(引用) 与 回收 垃圾回收器GC(Garbage Collection) 于1960年诞生在MIT的Lisp是第一门真正使用内存动态分配和垃圾收集技术的语 ...
- LINUX中如何查看某个端口是否被占用
之前查询端口是否被占用一直搞不明白,问了好多人,终于搞懂了,现在总结下: 1.netstat -anp |grep 端口号 如下,我以3306为例,netstat -anp |grep ...
- performSelector 的缺点
在内存管理方面容易有缺失.无法确定将要执行的选择子具体是什么,所以 ARC 无法插入适当的内存管理方法 选择子的返回类型只能是 id,最多有两个参数. 所以尽量避免使用这个东西. 下面来自苹果的文档 ...
- OC 中的属性
自动合成 (autosynthesis) @property 语法,会做下面两件事情 自动生成存取方法 由编译器生成,编辑器里不会看到这些方法. 向类中添加适当类型的实例变量 在属性前加下划线,作为实 ...