文件操作,直接上货~ !
1 文件操作 2 data = open("yesterday",encoding="utf-8").read() 3 print(data) 4 f = open("yesterday2",'r',encoding="utf-8") #文件句柄 5 -------------------------------------------------------------------------- 6 'a'=append追加 7 'a+'追加读写 8 'w'覆盖 9 'r+'读写,又能读又能写 10 'rb'二进制格式读取(网络传输) 11 'wb'写二进制 12 'w+'写读,不经常用 13 -------------------------------------------------------------------------- 14 15 f.write("\n成都成都~,\n") 16 f.write("啦啦啦") 17 f.close() 18 for i in range(10): 19 print(f.readline()) 20 print(f.readlines()) 21 22 low写法 23 24 for index,line in enumerate(f.readlines()): 25 if index == 9: 26 print("---------我是分割线-----------") 27 continue 28 print(line.strip()) 29 30 高级写法(计数器) 31 32 count = 0 33 for line in f: 34 count +=1 35 if count == 9: 36 print("-------------------------------") 37 # count +=1 38 continue 39 print(line) 40 41 f = open("yesterday2",'a',encoding="utf-8") #文件句柄 42 print(f.tell())#查看光标位置 43 # print(f.readline()) 44 print(f.readline()) 45 print(f.readline()) 46 print(f.readline()) 47 print(f.tell()) 48 f.seek(10) #光标回位(0即首行) 49 print(f.readline()) 50 print(f.encoding) 51 52 print(f.fileno())#返回文件句柄在内存中的编号 53 print(f.name)#打印文件名字 54 55 print(f.flush())#强制刷新(存钱场景) 56 flush刷新效果 57 import sys,time 58 for i in range(10): 59 sys.stdout.write('#') 60 sys.stdout.flush() 61 time.sleep(1) 62 63 截取 64 f = open("yesterday2",'a',encoding="utf-8") #文件句柄 65 f.truncate(15) #不写即清空;写即表示截断,如10则只保留前十个字符 66 f.flush() 67 68 f = open("yesterday2",'w+',encoding="utf-8") #文件句柄 69 f.write("-----------110------------\n") 70 f.write("-----------110------------\n") 71 f.write("-----------110------------\n") 72 print(f.tell()) 73 f.seek(10) 74 print(f.tell()) 75 print(f.readline()) 76 f.write("fffskfskfsdfsfsfsfsf") 77 f.close() 78 79 f = open("yesterday2",'wb') #文件句柄 80 f.write("vvsdvsdv\n".encode()) 81 f.close() 82 83 文件修改 84 85 f = open("yesterday2",'r',encoding='utf-8') 86 f_new = open("yesterday2.bak",'w',encoding='utf-8') 87 88 for line in f: 89 if "港岛妹妹" in line: 90 line = line.replace("港岛妹妹","小头儿子") 91 f_new.write(line) 92 f.close() 93 f_new.close() 94 95 96 with 自动关闭文件 97 f = open("yesterday2",'r',encoding='utf-8') 98 with open("yesterday2",'r',encoding='utf-8') as f ,\ 99 open("yesterday",'r',encoding='utf-8') as f2: 100 for line in f: 101 print(line)
编程思想:
1 #函数式编程 2 3 def fun1(): 4 #testing1 5 print('in the fun1') 6 return 0 7 #面向过程(实际就是没有返回值的函数) 8 def fun2(): 9 #testing2 10 print('in the fun2') 11 12 x=fun1() 13 y=fun2() 14 print("from fun1 return is %s"%x) 15 print("from fun2 return is %s"%y) 16 17 18 import time 19 def ll(): 20 time_format = '%Y-%m-%d %X' 21 time_current = time.strftime(time_format) 22 with open('a.txt','a+') as f: 23 f.write('%s end action\n'%(time_current)) 24 25 def test1(): 26 print("in the test1") 27 ll() 28 def test2(): 29 print("in the test2") 30 ll() 31 def test3(): 32 print("in the test3") 33 ll() 34 35 test1() 36 test2() 37 test3() 38 39 40 def test1(): 41 print("in the test1") 42 43 def test2(): 44 print("in the test2") 45 return 0 46 def test3(): 47 print("in the test3") 48 return 1,'hello',['fs','yy'],{'name':'dd'} 49 50 x=test1() 51 y=test2() 52 z=test3() 53 print(x) 54 print(y) 55 print(z)
局部参数,位置参数:
1 局部参数 2 def test(x,y):#形参 3 print(x) 4 print(y) 5 6 实参(关键参数不能写在位置参数前) 7 关键参数 8 test(x=1,y=2)#与形参顺序无关(关键参数调用) 9 10 位置参数 11 test(1,2)#与形参一一对应(位置参数调用) 12 13 默认参数(调用函数时,默认参数非必传) 14 用途:默认安装值 15 def test(x,y=2):#形参 16 print(x) 17 print(y) 18 test(1,3) 19 20 接受N个位置参数,转换成元组 21 def test(*args):#形参 22 print(args) 23 test(1,3,4) 24 test(*[1,2,3,4,5])# *args=*[1,2,3,4,5] 25 26 def test1(x,*args):#形参 27 print(x) 28 print(args) 29 test1(1,2,3,4,5,6) 30 31 把N个关键字参数转换成字典的方式 32 def test(**kwargs): 33 print(kwargs) 34 print(kwargs['name']) 35 print(kwargs['age']) 36 print(kwargs['sex']) 37 38 test(name='fs',age=8,sex='F') 39 test(**{'name':'fs','age':8}) 40 41 def test3(name,**kwargs): 42 print(name) 43 print(kwargs) 44 test3('fs',age=8,sex='F') 45 46 def test3(name,age=18,*args,**kwargs): 47 print(name) 48 print(age) 49 print(args) 50 print(kwargs) 51 test3('fs',34,[1,2,3],sex='F',hobby='tesla')
局部变量,全局变量、递归函数(及):
1 局部变量 2 3 def change_name(name): 4 print('before change',name) 5 name = 'FS' #这个函数就是这个变量的作用域 6 print('after change',name) 7 name = "fs" #小写 8 change_name(name)#转换成大写 9 print(name)#依旧打印小写 10 11 12 13 全局变量 14 15 school = 'OLdboy edu' 16 def change_name(name): 17 global school 18 school = 'Mage Linux' 19 print('before change',name,school) 20 name = 'FS' #这个函数就是这个变量的作用域 21 print('after change',name) 22 name = "fs" #小写 23 change_name(name)#转换成大写 24 print(name)#依旧打印小写 25 print('school:',school) 26 27 def change_name(): 28 global name 29 name = "FS" 30 31 change_name() 32 print(name ) 33 34 school = 'OLdboy edu' 35 names = ['fs','ww','rr'] 36 def change_name(): 37 names[0] = '妈妈咪呀' 38 print('limian',names) 39 change_name() 40 print(names) 41 42 递归(一个函数在内部调用自身本身,这个函数就是递归函数) 43 递归特性:1、必须有一个明确的结束条件 44 2、每进入更深一层递归时,问题规模相比上次递归都应有所减少 45 3、递归效率不高 46 def calc(n): 47 print(n) 48 if int(n/2) >0: 49 return calc(int(n/2)) 50 print(n) 51 calc(10) 52 53 54 一个函数可以接收另一个函数作为参数,这种函数就称为高阶函数 55 56 def add(x,y,f):#abs绝对值函数 57 return f(x)+f(y) 58 59 res = add(3,-6,abs) 60 print(res) 61 f = open (文件句柄) 62 r(只读) 63 w(覆盖写)慎用 64 a(追加) 65 a+(追加读) 66 r+(读写模式) 67 w+(写读模式,覆盖) 68 rb wb ab 69 f.close()
浙公网安备 33010602011771号