python 文件操作
文件操作基本语法: fp = open ("文件名","模式","字符编码集") fp 是文件io 对象(别名:文件句柄) i : input 写入 o : output 写出 1.文件的写入操作 (1) 打开文件 fp = open("ceshi1.txt",mode="w",encoding="utf-8") (2)写入内容 fp.write("把大象塞进去") (3) 关闭文件 fp.colse() 2.文件的读取操作 # (1) 打开文件 fp = open("ceshi1.txt",mode="r",encoding="utf-8") # (2) 读取内容 res = fp.read() print(res) # (3) 关闭文件 fp.close() # 将字符串和字节流(Bytes流)类型进行转换 (参数写成转化的字符编码格式) #encode() 编码 将字符串转化为字节流(Bytes流) #decode() 解码 将Bytes流转化为字符串 strvar = "你好" # 将字符串转化为字节流 encode res = strvar.encode("utf-8") print(res) # b'xe4xbdxa0xe5xa5xbd' # 将Bytes流转化为字符串 decode res2 = res.decode("utf-8") print(res2) # 三个字节代表一个中文字符; res = b"xe4xbdxa0".decode("utf-8") print(res) 3.wb 与 rb 来存储或 读取二进制字节流文件 '''如果使用了b二进制字节流模式,不要加任何的编码集''' # wb 写入字节流 # 打开文件 fp = open("ceshi2.txt",mode="wb") # 写入字节流 res = "我好想你,我滴dog".encode("utf-8") fp.write(res) # 关闭文件 fp.close() # rb 读取字节流 # 打开文件 fp = open("ceshi2.txt",mode="rb") # 读取字节流 res = fp.read() print(res) print(res.decode("utf-8")) # 关闭文件 fp.close() 4.字节流应用在图片,音频,视频的转化中.比如复制或存储操作 # 1.先把对应的字节流全部读出来 fp = open("集合.png",mode="rb") res = fp.read() print(res) fp.close() # 2.在吧读出来的字节流写入到另外一个文件中 fp = open("集合2.png",mode="wb") res = fp.write(res) fp.close() 文件的增强模式 + (utf-8编码格式下 默认一个中文三个字节 一个英文或符号 占用一个字节) #read() 功能: 读取字符的个数(里面的参数代表字符个数) #seek() 功能: 调整指针的位置(里面的参数代表字节个数) #tell() 功能: 当前光标左侧所有的字节数(返回字节数) # seek(0) 将光标直接移动到文件的开头 # seek(0,2) 将光标直接移动到文件的末尾 r+ 先读后写 '''read 是从当前光标的位置向后读取''' fp = open("ceshi3.txt",mode="r+",encoding="utf-8") res = fp.read() print(res) fp.write("gh") # 把光标位置移动到文件的开头 fp.seek(0) print(fp.read()) fp.close() r+ 先写后读 fp = open("ceshi3.txt",encoding="utf-8") fp.seek(0,2) fp.write("ijk") fp.seek(0) res = fp.read() print(res) fp.close() w+ 可读可写 fp = open("ceshi4.txt",mode="w+",encoding="utf-8") fp.write("abc") # 移动光标到文件行首 fp.seek(0) print(fp.read()) fp.close() a+ 可读可写 [如果写入内容,会强制文件指针指到文件末尾,默认在末尾追加内容.在读取文件时候,可以随意调整文件指针的位置] fp = open("ceshi5.txt",mode="a+",encoding="utf-8") fp.write("c") # 把光标移动到行首 fp.seek(0) print(fp.read()) # 调整光标在写入的时,无效,a+模式强制指针在最后; fp.seek(0) fp.write("p") fp.close() read tell seek (1)基本用法 fp = open("ceshi5.txt",encoding="utf-8") fp.seek(2) res = fp.read() print(res) # tell 返回的是所有的字节数,范围是当前光标左侧所有的字节数; res = fp.tell() print(res) # seek(移动到第2个[字节]) fp.seek(2) # read(读取 2个[字符]) res = fp.read(2) print(res) # tell 返回的当前光标左侧字节数; res = fp.tell() print(res) fp.close() (2) 注意点 fp = open("ceshi6.txt",encoding="utf-8") fp.seek(0) res = fp.read(1) print(res) # 要注意中文,如果移动的位置不是一个完整的中文,会报错@ fp.seek(5) fp.read() fp.close() (3)with 语法 => 可以自动完成close操作 as 起别名的意思 with open("ceshi6.txt",encoding="utf-8") as fp: fp.seek(0) res = fp.read() print(res) 文件函数 刷新缓冲区 flush 当文件关闭的时候自动刷新缓冲区 当整个程序运行结束的时候自动刷新缓冲区 当缓冲区写满了 会自动刷新缓冲区 手动刷新缓冲区 fp = open("ceshi5.txt",encoding="utf-8") fp.write("ddd") # 手动刷新缓冲区,直接把内容写入文件中. fp.flush() # 死循环 while True: pass fp.close() (1) 文件对象是迭代器,具有可迭代性; '''fp文件对象,每遍历依次,就读取一行.''' fp = open("ceshi7.txt",encoding="utf-8") for i in fp: print(i) print("<---------->") # 判断是否可读 print(fp.readable()) # 判断是否可写 print(fp.writable()) print("<---------->") 1.readline() 功能: 读取一行文件内容 # 基本写法1 with open("ceshi7.txt",encoding="utf-8") as fp: res = fp.readline() print(res) 循环所有内容 with open("ceshi7.txt",encoding="utf-8") as fp: ''' 先读出一行,如果不为空,直接打印,然后在读取一行进行判断 以此类推 直到最后读取的是空字符串'',循环终止; ''' res = fp.readline() while res: print(res) res = fp.readline() 2.readline(字符数) readline 后面的单位是字符数,如果字符数大于当前行所有的字符数,直接返回当前行 如果字符数小于当前行所有的字符数,直接打印当前字符个数; """ """ with open("ceshi7.txt",encoding="utf-8") as fp: res = fp.readline(300000) print(res) 3.readlines() 功能:将文件中的内容按照换行读取到列表当中 with open("ceshi7.txt",encoding="utf-8") as fp: lst_new = [] lst = fp.readlines() print(lst) # 过滤数据,把两边的空白符去掉; for i in lst: res = i.strip() lst_new.append(res) print(lst_new) 4.writelines() 功能:将内容是字符串的可迭代性数据写入文件中 参数:内容为字符串类型的可迭代数据 """ 1.数据类型是可迭代性数据 2.内容需要是字符串 """ with open("ceshi8.txt",encoding="utf-8") as fp: # lst = ["我爱你","亲爱的姑娘","见到你","我就心慌"] # lst = ["我爱你n","亲爱的姑娘n","见到你n","我就心慌n"] strvar = "abcedf" fp.writelines(strvar) 5.truncate() 功能: 把要截取的字符串提取出来,然后清空内容将提取的字符串重新写入文件中 (字节) '''先截取,在清空''' with open("ceshi8.txt",encoding="utf-8") as fp: # truncate(字节) fp.truncate(3) """ read(字符数) readline(字符数) seek(字节数) truncate(字节数) """ read 如果打开的模式是b字节流模式,读取的单位是字节; 如果正常打开,读取的是字符数; with open("ceshi9.txt",mode="wb") as fp: fp.write("你好".encode("utf-8")) # 读取 with open("ceshi9.txt",mode="rb") as fp: res = fp.read(3) print(res.decode()) 文件的修改[推荐使用] import os with open('a.txt','r',encoding='utf-8') as read_f,open('.a.txt.swap','w',encoding='utf-8') as write_f: for line in read_f: line=line.replace('222','111') write_f.write(line) os.remove('a.txt') os.rename('.a.txt.swap','a.txt') (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |