# 文本文件: # 自动解码成文字的文件 # 二进制文件: # 专门软件进行读写,无法进行文字解析 """ 文件读写 过程: 1.打开 open(文件,mode,encoding=) close关闭 with open(文件,mode,encoding) 自动关闭 2.处理 3.关闭 读: 文件对象.read() """ from pathlib import Path # t 文本模式 (默认)。 # x 写模式,新建一个文件,如果该文件已存在则会报错。 # b 二进制模式。 # + 打开一个文件进行更新(可读可写)。 # U 通用换行模式(Python 3 不支持)。 # r 只读,指针在文件开头。默认模式。文件不存在是报错 # rb 二进制格式只读。指针在文件开头。一般用于非文本文件如图片等。 # r+ ####读写。指针在文件开头。文件不存在是报错 # rb+ 二进制格式读写。指针在文件开头。一般用于非文本文件如图片等。 # w 写入。已存在则打开文件,不存在则创建新文件。指针在文件开头。清空原有数据 # wb 二进制格写入。已存在则打开文件,不存在则创建新文件。指针在文件开头。清空原有数据。一般用于非文本文件如图片等。 # w+ ####读写。已存在则打开文件,不存在则创建新文件。指针在文件开头。清空原有数据 # wb+ 二进制格式读写。已存在则打开文件,不存在则创建新文件。指针在文件开头。清空原有数据。一般用于非文本文件如图片等。 # a 追加。文件已存在指针在文件结尾。文件不存在,创建新文件进行写入。 # ab 二进制追加。文件已存在,指针将在结尾。文件不存在,创建新文件进行写入。 # a+ ####读写。文件已存在,指针在结尾。文件不存在,创建新文件用于读写。 # ab+ 二进制读写。文件已存在,指针在结尾。文件不存在,创建新文件用于读写。 # 1.open打开 #编码: windows 默认的是gbk,linux默认utf-8 file_object1 = open("路径管理.py", "r", encoding='utf-8') try: # 2.处理 pass # print(file_object1.read()) finally: # 3.关闭 file_object1.close() # 文件处理是否成功,必须关闭文件 # with 打开 with open("路径管理.py", "r", encoding='utf-8') as file_object2: pass # print(file_object2.read()) # cpu离开代码块,自动关闭 """ 读取 按字符读:read(字符数) 默认读取全部 按行读取:for line in file :循环读取每行 """ with open("路径管理.py", "r", encoding='utf-8') as file: # print(file.read()) # 读取全部 for line in file: pass # print(line) # 读取每行 # 获取文件长度 total_len = 0 for item in Path.cwd().parent.parent.rglob('*.py'): with open(item, "r", encoding='utf-8') as f: total_len += len(f.read()) print(total_len) """ 管理文件 读写文件 f.seek() 可以手动改变文件指针位置 """ # 读 # 查找含义关键词的py文件 content = input('想查找的关键词:') for item in Path.cwd().parent.parent.rglob('*.py'): with open(item, "r", encoding='utf-8') as f: if content in f.read(): print(item) # 写 with open('文件管理操作实例/a.txt', 'w', encoding='utf-8') as f: f.write("a" + '\n') # 按字符写入 f.write("b" + '\n') # 换行用+"\n" f.write("c" + '\n') ############### class StudentModel: def __init__(self, name="", age=0, score=0, sid=0): self.name = name self.age = age self.score = score self.sid = sid # 全球唯一标识符 def __repr__(self): return "StudentModel('%s',%s,%s,%s)" % (self.name, self.sid, self.age, self.score) """将一个自定义对象,存入文件并读取 """ jy = StudentModel('金庸', 26, 100, 101) print(jy) # 1.打开(创建)文件 with open('文件管理操作实例/student.txt', 'w', encoding="utf-8") as file: # 2.写入自定义对象的repr file.write(jy.__repr__() + '\n') # 3.打开文件 with open('文件管理操作实例/student.txt', "r", encoding="utf-8") as file: new = eval(file.read()) print(new.name) """将多个自定义对象,存入文件并读取 """ student_list = [ StudentModel('狗哥', 26, 100, 101), StudentModel('古龙', 13, 10, 103), StudentModel('猪猪', 55, 88, 102), ] with open('文件管理操作实例/student.txt', 'a+', encoding="utf-8") as file: for item in student_list: file.write(item.__repr__() + '\n') with open('文件管理操作实例/student.txt', "r", encoding="utf-8") as file: all_info = list(file.readlines()) for item in all_info: print(eval(item).name) """ 文件操作复习 操作模式: r 读取 w 写入 a 追加 with open(文件路径,操作模式,encoding = 编码方式) as 文件对象: 文件对象.read() # 读 文件对象.write(自定义对象__.repr___) #写 自定义对象 = eval(文件对象.read()) # eval 执行代码文本 """ """ 二进制文件 内部编码为二进制码,无法通过文字编码解析,如压缩包,音频,图片等 """ from pathlib import Path # 获取文件路径 path = 0 for item in Path.cwd().rglob('*'): if item.name == "内存图1.jpg": path = Path(item) # 读取路径中的文件 with open(path, 'rb') as f: data = f.read() # 将文件写到当前路径 # 相当于 搜索 与 复制 with open('new_pic1.jpg', 'wb') as f: f.write(data) """# 文件加密""" path = 0 for item in Path.cwd().rglob('*'): if item.name == "内存图1.jpg": path1 = Path(item) # 读取路径中的文件 with open(path, 'rb') as f: data = f.read() # 写入加密后新文件 with open('加密文件.jpg', 'wb') as f: f.write(b'asf') # 写入干扰字符 f.write(data) # 解密 with open('加密文件.jpg', 'rb') as f: f.read(3) # 读取干扰字符 img_content = f.read() # 读取干扰字符后面的文件 with open('解密文件.jpg', 'wb') as f: f.write(img_content) # 写入删除干扰字符的内容
Python重点知识八:文件读写(Path模块)
于 2023-04-11 09:21:33 首次发布