一:接上次知识(内置函数)
21.type 根据数据类型不同,执行不同逻辑
msg='123' if type(msg) is str: msg=int(msg) res=msg+1 print(res) def test(): msg='fghjk.' print(locals()) test()
22.import 导入模块
#文件处理 import test test.say_hi() #import 不能导入字符串,而__import 导入的就是字符串类型 module_name='test' m=__import__(module_name) m.say_hi()
二:文件处理
1.文件处理流程
- 打开文件,得到文件句柄并赋值给一个变量
- 通过句柄对文件进行操作
- 关闭文件
2.文件基本操作
#文件处理读操作 f=open('xxx',encoding='utf-8') data=f.read() print(data) #f.close() print(f.readable()) print(f.readline()) f.close() #文件处理写操作 f=open('xxx','w',encoding='utf8') # f.read() f.write('1111111 ') f.write('2222222 ') # f.writable() f.writelines(['5555 ','666 '])#只能是字符串 f.closed #追加 f=open('xxx','a',encoding='utf-8') f.write('写到文件最后') f=open('xxx','r+',encoding='utf-8') data=f.read() print(data) f.write('123sb') #修改文件内容 src_f=open('xxx','r',encoding='utf-8') data=src_f.readlines() src_f.close() print(data) dst_f=open('xxx','w',encoding='utf-8') dst_f.writelines(data[0]) dst_f.close() #不用f.close() with open('a.txt','w') as f: f.write('111 ') #将原文件内容写到新文件中去 src_f=open('xxx','r',encoding='utf-8') dst_f=open('xxx','w',encoding='utf-8') with open('xxx','r',encoding='utf-8') as src_f, open('xxx_new','w',encoding='utf-8') as dst_f: data=src_f.read() dst_f.write(data)
3.map reduce filter 函数回顾
l=[1,2,3,4,5] print(list(map(str,l))) #转换成字符串 from functools import reduce l=[1,2,3,4,5] res=reduce(lambda x,y:x+y,l,3) #合并在一起 print(res) name=['alex_sb','linhaifeng'] res=filter(lambda x:not x.endswith('sb'),name) print(res) print(list(res)) #过滤
4.文件操作复习
f=open('test.py','w',encoding='utf-8') f.write('111 ') f.write('222 ') f.write('333 ') f.close() f=open('test.py','a',encoding='utf-8') f.write('这是模式') f.close() f=open('test.py','r+',encoding='utf-8') f.write('hello') #读写模式 从光标开始的地方写
5.文件处理b模式
以b方式打开时,读取到的内容是字节类型,写入时也需要提供字节类型,不能指定编码
#文件处理b模式 # f=open('test.py','rb',encoding='utf-8')#b的方式不能指定编码,报错 f=open('test.py','rb')#b的方式不能指定编码 data=f.read() # '字符串'----------encode---------bytes # bytes--------------decode---------'字符串' print(data.decode('utf-8')) #看见文件内容 f.close() f=open('test22.py','wb')#b的方式不能指定编码 f.write(bytes('11111 ',encoding='utf-8')) f.write('样件'.encode('utf-8')) f=open('test22.py','ab')#a代表在文件的最后一个位置 f.write('样件'.encode('utf-8'))
6.文件操作其他方式
#文件操作其他方式 f=open('a.txt','r+',encoding='utf-8',newline='')#读取文件中真正的换行符号 data=f.read() print(data) f=open('b.txt','r+',encoding='gb2312') f.write('你好啊') f.flush()#刷新。文件操作是通过软件将文件从硬盘读到内存,flush即,强制将写入的数据刷到硬盘 #文件内光标移动 #read(3)代表读取3个字符,其余的文件内光标移动都是以字节为单位如seek,tell,read,truncate print(f.readlines()) print(f.tell()) f.readline() print(f.tell()) f.seek(3) #光标移动到3个字节后开始读取 print(f.tell()) data=f.read(4)# 以字符读取 print(data) f.truncate(10) #截取
6. seek高端补充
seek三种模式:
- 0:文件开始
- 1:相对位置
- 2:从文件末尾
而 1.2模式必须以b模式运行
f=open('seek.txt','rb') print(f.tell()) f.seek(-5,2) #三种模式0:文件开头 1:相对位置 2:从文件末尾 print(f.read()) #光标从文件后开始读到第5个开始,f.read即从当前位置读到最后 f.seek(3,1) print(f.tell()) #读取日志文件最后一行 f=open('日志文件','rb') for i in f: offs=-10 while True: f.seek(offs,2) data=f.readlines() if len(data) > 1: print('文件最后一行是%s' %(data[-1].decode('utf-8'))) break offs*=2