python 高级
2017-01-22 16:38
316 查看
使用到的函数在python3.5是open,在python2.7同时支持file和open
多个装饰器装饰同一个函数。
python正则
python字符串格式化
python迭代器生成器
python反射
python设计模式
python异常处理
打开方式
文件句柄=open('文件路径','打开模式')文件句柄相当于变量名。打开文件的模式r :只读,文件必须存在w:只写,文件不存在则创建,存在则清空x:只写,文件不可读,不存在则创建,存在则报错a:追加, 文件不存在则创建,存在则在末尾添加内容r+:读写w+:写读x+:写读a+:写读rb:二进制读wb:二进制写xb:二进制只写ab:二进制追加以b方式打开的,读取到的内容是字节内心,写入时也需要提供字节类型rb+:二进制读写wb+:二进制读写xb+:二进制只写ab+:二进制读写文件读取方式
read([size]):读取文件全部内容,设置了size,读取size字节readline([size]):一行一行的读取readlines():读取到的每一行内容作为列表中的一个元素文件写入方式
write(str):将字符串写如文件writelines(sequence or strings):写多行到文件,参数可以是一个可迭代的对象、列表、元组等。文件操作的方法
closeflushisattyreadabletellseekseekablewritable同时打开多个文件
避免打开文件后忘记关闭,可以通过管理上下文with open('log', 'r') as f: statement当with执行完毕时,内部会自动关闭并释放文件资源。with又支持同时对多个文件的上下文管理with open('log1') as obj1, open('log2') as obj2: statementpython装饰器
装饰器是由函数去生成的,用于装饰某个函数或者方法或者类,他可以让这个函数在执行之前或者执行之后做一些操作。#!/usr/bin/env python # _*_ coding: utf-8 _*_ def decorator(func): # 创建一个装饰器函数,接受的参数arg参数就是func函数名 def inner(*args, **kwargs): print("执行函数之前") ret = func(*args, **kwargs) print("执行函数之后") return ret return inner @decorator # 如果要让某个函数使用装饰器,只需要在这个函数上面加上@+装饰器名 def func(arg): print(arg) func("Hello World!")
多个装饰器装饰同一个函数。
#!/usr/bin/env python # _*_ coding: utf-8 _*_ def decorator1(func): def inner(): print("开始之前执行装饰器01") ret = func() print("结束之后执行装饰器01") return ret return inner def decorator2(func): def inner(): print("decorator2>>>Start...") ret = func() print("decorator2>>>End...") return ret return inner @decorator1 @decorator2 def index(): print("执行函数...") index()
python正则
python字符串格式化
python迭代器生成器
python反射
python设计模式
python异常处理
相关文章推荐
- Python中Array的常用操作(三)数组高级操作
- 用于 Python 的高级 GUI 库(Qt 和 PyQt)
- Python 正则表达式高级应用举例
- c++和Python互操作高级应用(5)
- 参数文件描述符[Python]Socket高级 -- select I/O复用模型(一)
- python翻译[排序高级]
- 培训python多少钱?高级python培训
- python高级函数模块[1]
- 高级数据类型-python
- python高级进阶篇——FTP服务器开发简述
- 新头衔:热酷高级python软件工程师,你问我去热酷干嘛?
- python类库32[多进程共享高级之Manager]
- C语言中执行python代码或源程序文件(高级嵌入方式)
- python26高级[读写Excel]
- c++和Python互操作高级应用(5)
- Python 3 初探,第 2 部分: 高级主题
- 新头衔:热酷高级python开发工程师,问我去热酷干嘛?
- Python高级特性
- [Python]Socket高级 -- select I/O复用模型(一)
- [Python]Socket高级 -- select I/O复用模型(二)