文件操作高级、函数高级05
一、控制文件内指针的移动
# f.seek(字节个数,模式) # 模式有三种 # 0:参照文件的开头 # 1:参照当前所在的位置 # 2:参照文件末尾的位置 # 注意: # 1、无论何种模式,都是以字节单位移动,只有t模式下的read(n)的n代表的是字符个数 # with open('a.txt',mode='rt',encoding='utf-8') as f: # data=f.read(6) # print(data) # with open('a.txt',mode='rt',encoding='utf-8') as f: # data = f.read(6) # print(data) # with open('a.txt',mode='rt',encoding='utf-8') as f: # data = f.read(6) # print(data) #打印的是字符hello美 # with open('a.txt',mode='rb') as f: # data = f.read(6) # print(data) #显示的是字节b'hello\xe7' # with open('a.txt',mode='rb')as f: # data = f.read(6) # print(data) # with open('a.txt',mode='rb') as f: # data=f.read(5) # print(data.decode('utf-8')) # with open('a.txt',mode='rb',)as f: # data = f.read(5) # print(data.decode('utf-8')) # with open('a.txt',mode='rb') as f: # data = f.read(5) # print(data.decode('utf-8')) # with open('a.txt',mode='rb') as f: # data = f.read(5) # print(data.decode('utf-8')) # 2、只有0模式可以在t模式下使用,而0、1、2都可以在b模式下用 # 示例 # with open('a.txt',mode='rt',encoding='utf-8') as f: #t模式下1,2模式会报错 # f.seek(3,2) # with open('a.txt',mode='rb',encoding='utf-8') as f: # f.seek(0,0) # with open('a.txt',mode='rb') as f: # f.seek(6,0) # print(f.read().decode('utf-8')) # f.seek(16,1) # print(f.tell()) # with open('a.txt',mode='rb')as f: # f.seek(5,0) # print(f.read().decode('utf-8')) # with open('a.txt',mode='rb') as f: # f.seek(8,0) # print(f.read().decode('utf-8')) # f.seek(19,1) # print(f.tell()) # f.seek(-3,2) # print(f.read().decode('utf-8')) # f.seek(0,2) # print(f.tell()) # with open('b.txt',mode='wt',encoding='utf-8') as f: # f.seek(10,0) # print(f.tell()) # f.write("你好") # 应用1:tail -f access.log # import time # with open('access.log',mode='rb') as f: # f.seek(0,2) # while True: # line=f.readline() # if len(line) == 0: # time.sleep(0.3) # else: # print(line.decode('utf-8'),end='') # import time # with open('abuse.log',mode='rb')as f: # f.seek(0,2) # while True: # line = f.readline() # if len(line) == 0: # time.sleep(0.3) # else: # print(line.decode('utf-8'),end='')
run.py
# import time # # with open('access.log',mode='at',encoding='utf-8') as f: # f.write("%s %s\n" %(time.strftime("%Y-%m-%d %H:%M:%S %p"),"egon给alex转了3个亿")) # import time # with open('abuse.log',mode='at',encoding='utf-8')as f: # f.write('%s %s\n'%(time.strftime('%Y-%m-%d %H:%M%S%p'),'吴常文给姜高转了一百个亿'))
二、文件操作的其他方法
# with open('b.txt',mode='rt',encoding='utf-8') as f: # data=f.readlines() # print(data,end=' ') # print() with open('b.txt',mode='wt',encoding='utf-8') as f: # f.write("1111\n222\n3333\n") lines=["aaaa\n",'bbb\n','cccc\n'] # for line in lines: # f.write(line) # # f.writelines(lines) # f.writelines("hello") # f.write("hello") # with open('b.txt',mode='wt',encoding='utf-8') as f: # f.write('hello\n') # f.write('world\n') # f.flush() with open('b.txt',mode='r+t',encoding='utf-8') as f: f.truncate(4)
三、可变长参数
def index(x,y,z): print(x,y,z) def wrapper(*args,**kwargs): # args=(1,2,3,4,5) kwargs={"a":1,"b":2} index(*args,**kwargs) # index(*(1,2,3,4,5),**{"a":1,"b":2}) # index(1,2,3,4,5,a=1,b=2) # index(x=1,y=2,z=3) wrapper(x=1,y=2,z=3)
四、函数对象
# 函数是第一个等公民:可以把函数当变量去用 def func(): # func=函数的内存地址 print('from func') x=10 # x=10的内地址 #1、可以被赋值 # f=func # f() #2、可以当作参数传给另外一个函数 # def foo(f): # print(f) # f() # # foo(func) #3、可以当作函数的返回值 # def foo(f): # return f # res=foo(func) # print(res) #4、可以当作容器类型的元素 # l=[func,] # print(l) # l[0]() def login(): print('登录...') def register(): print('注册...') def tranfer(): print('转账...') def withdraw(): print("提现功能。。。") func_dic={ "1":["登录",login], "2":["注册",register], "3":["转账",tranfer], "4":["提现",withdraw] } while True: for k,v in func_dic.items(): print(k,v[0]) choice = input("请输入您的命令编号,输入0退出:").strip() if choice == "0": break if choice in func_dic: func_dic[choice][1]() else: print("输入的指令错误")
五、补充
def add(x:int,y:int) -> int: return x+y # res=add("aaa","bbbb") # print(res) # help(add)
六、函数嵌套
# 1、函数的嵌套调用:在调用一个函数的过程中又调用了其他函数 # def bar(): # print('from bar') # # def foo(): # print('from foo') # bar() # # foo() # 应用示例 # def max2(x,y): # if x > y: # return x # else: # return y # # def max4(a,b,c,d): # res1=max2(a,b) # res2=max2(res1,c) # res3=max2(res2,d) # return res3 # # print(max4(1,2,3,4)) # 2、函数的嵌套定义:在一个函数内部又定义了其他函数 # 特点:定义在函数内的函数通常情况只能函数内部使用,这是一种封闭的效果 # def f1(): # def f2(): # print('from f2') # # x = 111 # # print(x) # # print(f2) # f2() # f1() # 应用示例 # from math import pi # # def circle(radius,action=1): # def perimeter(radius): # return 2 * pi * radius # # def area(radius): # return pi * (radius * radius) # # if action == 1: # return perimeter(radius) # elif action == 2: # return area(radius) # # print(circle(10,1)) # print(circle(10,2)) # 函数嵌套定义+函数对象 def f1(): def f2(): print('from f2') return f2 res=f1() print(res) res()
七、名称空间与作用域
# 一:名称空间:就是用来存放名字的内存空间 # 名称空间分为三大类: # 1、内置名称空间:存放python解释器提供的名字 # 生命周期:python解释器启动则产生,python解释器关闭则销毁 # 2、全局名称空间:顶级的名字 # 生命周期:开始python程序则启动,python程序运行完毕则销毁 # x = 1 # y = 2 # if True: # z = 3 # # while True: # bbb = 44 # # 3、局部名称空间:在函数内部定义的名字 # 生命周期:调用函数则产生,函数调用结束则会立即销毁 # def f1(aaa): # # aaa=555 # def f2(): # ccc = 666 # # f1(555) # 名字访问的优先级:基于当前所在的位置向上查找(局部-》全局-》内置) # 例1 # # len=111 # # def f1(): # # len=222 # def f2(): # # len=333 # print(len) # f2() # # f1() # 例2 # def f1(): # print(len) # # # f1() # len=111 # # f1() # # def foo(): # len=333333 # f1() # # # def bar(): # len=44444 # f1() # # foo() # bar() # 例3: # aaa=333 # # def f1(): # # print(aaa) # # print(len) # x=111 # def ff1(): # print("fff1===>x: ",x) # ff1() # # def f2(): # # print(aaa) # # print(len) # y=222 # # f1() # f2() # # 例4: # len=111 # # def f1(): # print(len) # # # # def f2(): # len=33333333333333333333 # f1() # # f2() # 名称空间的嵌套关系是在函数定义阶段扫描语法的时候生成的 # x=111 # # def func(): # print(x) # y=2222 # # func() # 二:作用域 # 全局作用域:内置+全局名称空间中的名字 # 特点:全局存活、全局有效 # 局部作用域:局部名称空间空间的名字 # 特点:临时存活,局部有效 # LEGB # 三:global、nonlocal # 3.1 global:在函数内声明名字是来自于全局的 # l=[1,2,3] # def func(): # l[0]=111 # func() # print(l) # x=10 # def func(): # global x # x=20 # func() # print(x) # 3.2 nonlocal在函数内声明名字是来自于外层函数的 # x=333 # def f1(): # # x=111 # def f2(): # # global x # nonlocal x # x=222 # f2() # print(x) # # f1() def f1(): x=111 def f2(): print('from f2',x) return f2 res=f1() def foo(): x=222 res() foo()
八、闭包函数
# 闭包函数=函数嵌套定义+名称空间与作用域+函数对象 # 闭:指的是该函数是一个定义在函数内部的函数 # 包:闭函数访问了一个来自于外层函数中的名字 # def f1(): # x = 111 # def f2(): # print('from f2: ',x) # # return f2 # 千万不要加括号 # # res=f1() # # def foo(): # x=222 # res() # # foo() # 为函数体传参的解决方案: # 方案一:直接定义形参 # def f2(x): # print(x) # # f2(111) # f2(222) # f2(333) # 闭包函数是一种为函数体传参的解决方案 # def f1(x): # # x=111 # def f2(): # print(x) # # return f2 # return f1.locals.f2的内存地址 # # f2=f1(111) # f2() # 写死? # 专用? # 传值? def outter(x): # x =2222 def wrapper(): print(x) return wrapper f=outter(333) # print(f) f()
九、装饰器
""" 1 什么是装饰器 器=》工具=》函数 装饰=》指的是为被装饰者添加新功能 2、为何要用装饰器 开放封闭原则: 封闭指的是对修改源代码是封闭的 开放指的是对拓展新功能是开放的 装饰器就是一个函数,该函数就是在不修改被装饰对象源代码以及调用的方式的前提下,为被装饰对象添加额外的功能 3、如何实现装饰器=》闭包函数 装饰器的目标:为被装饰对象添加额外的新功能 装饰器的实现应该遵循的原则: 1、不该被装饰对象的源代码 2、不该被装饰对象的调用方式 """ # # 例1: # import time # # def index(): # start=time.time() # print("index===>") # time.sleep(0.5) # stop=time.time() # print("run time is : %s" %(stop - start)) # # index() # # 例2: # import time # # def index(): # print("index===>") # time.sleep(0.5) # # # start=time.time() # index() # stop=time.time() # print("run time is : %s" %(stop - start)) # # 例3: # import time # # def index(): # print("index===>") # time.sleep(0.5) # # # def wrapper(): # start=time.time() # index() # stop=time.time() # print("run time is : %s" %(stop - start)) # # wrapper() # # 例4:直接通过参数为函数体传参 # # import time # # # # def index(): # # print("index===>") # # time.sleep(0.5) # # # # # # def wrapper(func): # # start=time.time() # # func() # # stop=time.time() # # print("run time is : %s" %(stop - start)) # # # # wrapper(index) # # 例5:直接通过参数为函数体传参 # import time # # def index(): # print("index===>") # time.sleep(0.5) # # # def outter(func): # # func=index的内存地址 # def wrapper(): # start=time.time() # func() # stop=time.time() # print("run time is : %s" %(stop - start)) # return wrapper # 千万不要加括号 # # index=outter(index) # # index() # wrapper() # 例6: # import time # # def index(x,y,z): # print("index===>",x,y,z) # time.sleep(0.5) # return 123 # # def home(name): # print('welcome %s to home page' %name) # time.sleep(1) # # # index(1,2,3) # # home("egon") # # def outter(func): # # func=index的内存地址 # def wrapper(*args,**kwargs): # start=time.time() # res=func(*args,**kwargs) # stop=time.time() # print("run time is : %s" %(stop - start)) # return res # return wrapper # 千万不要加括号 # # index=outter(index) # res=index(1,2,3) # wrapper(1,2,3) # print(res) # # home=outter(home) # res=home("egon") # print(res) # 例7:语法糖 # import time # from functools import wraps # # def timmer(func): # @wraps(func) # def wrapper(*args,**kwargs): # start=time.time() # res=func(*args,**kwargs) # stop=time.time() # print("run time is : %s" %(stop - start)) # return res # # wrapper.__name__=func.__name__ # # wrapper.__doc__=func.__doc__ # return wrapper # 千万不要加括号 # # @timmer # index=timmer(index) # def index(x,y,z): # """ # index的文档注释 # """ # print("index===>",x,y,z) # time.sleep(0.5) # return 123 # # @timmer # home=timmer(home) # def home(name): # """ # home的文档注释 # """ # print('welcome %s to home page' %name) # time.sleep(1) # # # res=index(1,2,3) # wrapper(1,2,3) # # print(res) # # # # res=home("egon") # # print(res) # # # # help(index) # print(index.__name__) # 例8: def auth(func): def wrapper(*args,**kwargs): name=input("username>>: ").strip() pwd=input("password>>: ").strip() if name == "egon" and pwd == "123": print('login successful') res=func(*args,**kwargs) return res else: print("username or password error") return wrapper @auth def index(): print('from index') index()