Python 文件处理
python对文件操作时的过程分析:
流程分析:
1:向操作系统发起系统调用
2:操作系统打开这个文件,返回一个文件句柄给应用程序
3: 在应用程序中把文件句柄赋值给一个变量
注意两点:
1:打开一个文件对应两部分,一个Python级别的文件句柄,另外一个是操作系统打开的文件(默认
打开文件的编码是以操作系统的编码为准的,除非open()指定encoding='编码' )
2:当文件操作完毕后,应该回收两部分资源,
del f:回收应用程序资源(python解释器自动的垃圾回收机制已经替我们做了)
f.close:回收操作系统
打开 f=open('a.txt',mode='r',encoding='utf-8') 读/写 data=f.read() print(data) 关闭 del f #回收python资源 f.close() #回收操作系统的资源 del f print(f)
使用with 不用考虑文件回收资源
上下文管理with with open('a.txt',mode='r',encoding='utf-8') as ff: print(ff.read())
对文件操作 r 只读
r:默认的打开模式,只读,文件不存在则报错 f=open('a.txt',encoding='utf-8') print('===>',f.read()) #读所有,bytes---decode('utf-8')--->str print('===>',f.read()) print(f.readlines()) #读所有,结果放入列表中 print(f.readline(),end='') #一次读一行 print(f.readline(),end='') print(f.readline(),end='') print(f.readline(),end='') f.close()
对文件操作 w 只写
w:只写模式,如果文件存在则清空,如果文件不存在则新建 f=open('b.txt',mode='w',encoding='utf-8') f.write('11111\n') #unicode---encode-->bytes f.write('2222\n') f.write('333333\n') l=['444\n','55555\n','66666\n'] for line in l: f.write(line) f.writelines(['444\n','55555\n','66666\n']) f.close()
对文件操作 a 追加
a:追加写模式,如果文件存在则把光标移动到文件末尾,如果文件不存在则新建 f=open('c.txt','a',encoding='utf-8') f.write('333333\n') f.write('444444\n') f.writelines(['5555\n','666\n']) f.close()
遍历文件
with open('a.txt',encoding='utf-8') as f: #不推荐使用 # lines=f.readlines() # for line in lines: # print(line,end='') #推荐使用 for line in f: print(line,end='')
以bytes的形式去操作文件内容,不能指定编码
b:以bytes的形式去操作文件内容,不能指定编码 with open('yuanhao.jpg',mode='rb') as f: print(f.read().decode('utf-8')) with open('a.txt',mode='rb') as f: data=f.read() print(data.decode('utf-8')) with open('d.txt',mode='wb') as f: f.write('哈哈哈hello'.encode('utf-8')) with open('d.txt', mode='ab') as f: f.write('哈哈哈hello'.encode('utf-8'))
常用的对文件的操作:
read 以文本的模式读文件,n代表的是字符的个数 with open('a.txt','r',encoding='utf-8') as f: data=f.read(3) print(data) 以b的模式读文件,n代表的是字节的个数 with open('a.txt','rb') as f: data=f.read(3) print(f.tell()) print(data.decode('utf-8')) tell:告诉当前光标的位置 with open('a.txt','r',encoding='utf-8') as f: data=f.read(3) print(f.tell()) print(data) seek:移动光标 with open('a.txt','r',encoding='utf-8') as f: data1=f.read() print('first: ',data1) print(f.tell()) f.seek(0) data2 = f.read() print('second: ',data2) 0:文件开头 1:当前位置 2:文件末尾 with open('a.txt','r',encoding='utf-8') as f: f.seek(3,0) print(f.read()) with open('a.txt', 'rb',) as f: f.read(3) f.seek(3,1) # print(f.read()) print(f.read().decode('utf-8')) with open('a.txt', 'rb',) as f: f.seek(-3,2) print(f.read()) tail -f access.log with open('access.log','a',encoding='utf-8') as f: f.write('11111\n') truncate with open('a.txt','a',encoding='utf-8') as f: f.truncate(2)
把文件当做数据库用:
with open('db.txt','r',encoding='utf-8') as f: for line in f: user_l=line.split(',') print(user_l[1],int(user_l[2]))
模拟linux cp 功能:
with open('a.txt','rb') as r_a,open('b','wb') as w_b: for list in r_a: w_b.write(list)
模拟linux mv 功能:
import os with open('c','r') as r_f,open('b','w') as w_f: for list in r_f: data=list.replace('333311','44444') w_f.write(data) os.remove('c') os.rename('b','c')
练习题:
文件a.txt内容:每一行内容分别为商品名字,价钱,个数,求出本次购物花费的总钱数
apple 10 3
tesla 100000 1
mac 3000 2
lenovo 30000 3
chicken 10 3
with open('a.txt','r',encoding='utf-8') as f: for list in f: c=list.split( )[0] a=int(list.split( )[1]) b=int(list.split( )[2]) print(c,a*b)