当前位置:首页>python>Python读写文本(二)

Python读写文本(二)

  • 2026-09-22 11:24:22
Python读写文本(二)
之前学习了Python打开文本的方法,使用open()函数,
Python 读写文本(一)
# 1. 打开文件f = open("文件路径", "r", encoding="utf-8")# 2. 读取内容content = f.read()# 3. 关闭文件(必须!否则会占用系统资源)f.close()
但是使用open()必须添加close()方法,如果读取 / 写入过程中抛出异常(比如编码错误、文件损坏),f.close() 不会执行,导致文件句柄泄露,系统会认为文件还在被使用,无法删除 / 修改,直到 Python 进程结束。推荐使用with open,可自动关闭文件,无需代码中添加close()例如:
import osdir_path = r'C:\Users\86182\Desktop\read_file'file_name = "test_file.k"file_path = os.path.join(dir_path,file_name)try:    with open(file_path,"r",encoding="utf-8") as f:        content = f.read()print(content)except Exception as e:print(f"open failed due to {e}")
with open(file_path,"r",encoding="utf-8") as f:中的“as f”是将open()返回的文件对象赋值给变量f;通过变量f可以调用文件的读/写方法。
读写文件的方法如下:
方法
作用
适用场景
关键注意点
f.read(size=-1)
读取 size 个字符(默认 - 1,读取全部)
小文件一次性读取
大文件用会占满内存,size 可指定读取长度(如 1024)
f.readline(size=-1)
读取一行内容(最多 size 个字符)
逐行读取、手动控制读取进度
读完一行后指针移到下一行,到末尾返回空字符串
f.readlines(hint=-1)
读取所有行到列表(hint 为预估字符数,减少内存分配)
需随机访问某一行(如取第 5 行)、文件体积适中
大文件慎用,会加载所有行到内存
f.__iter__()
迭代器(支持 for line in f)
大文件逐行读取(最优方式)
内存占用极低,无需手动调用,循环自动触发
方法
作用
注意点
f.write(string)
写入字符串到文件,返回写入的字符数
需用w(覆盖)/a(追加)/r+(读写)模式
f.writelines(list)
写入字符串列表到文件(每行一个元素,需手动加换行符\n)
不会自动加换行,需lines = [line+'\n' for line in lines]
f.flush()
强制将缓冲区内容写入磁盘(无需等待文件关闭)
避免数据滞留缓冲区导致丢失
方法
作用
示例
f.tell()
返回当前指针位置(单位:字节,文本模式下和字符数可能不一致)
pos = f.tell()
 → 查看当前光标位置
f.seek(offset, whence=0)
移动指针到指定位置:- offset:偏移量(字节)- whence:0 = 文件开头,1 = 当前位置,2 = 文件末尾
f.seek(0)
 → 指针回到文件开头(重读)rstr
源文本内容如下:
读取全部文本(read)代码如下:
import osdir_path = r'C:\Users\86182\Desktop\read_file'file_name = "test_file_01.k"file_path = os.path.join(dir_path,file_name)try:    with open(file_path,"r",encoding="utf-8") as f:        content = f.read()print(content)except Exception as e:print(f"open failed due to {e}")
运行代码后输出如下:
读取前5个字符代码如下:
import osdir_path = r'C:\Users\86182\Desktop\read_file'file_name = "test_file_01.k"file_path = os.path.join(dir_path,file_name)try:    with open(file_path,"r",encoding="utf-8") as f:        content = f.read(5)print(content)except Exception as e:print(f"open failed due to {e}")
运行代码后输出如下:
按行读取(readline),读取前两行代码如下:
import osdir_path = r'C:\Users\86182\Desktop\read_file'file_name = "test_file_01.k"file_path = os.path.join(dir_path,file_name)try:    with open(file_path,"r",encoding="utf-8") as f:        first_line = f.readline()print(first_line)        second_line = f.readline()print(second_line)except Exception as e:print(f"open failed due to {e}")
运行代码后输出如下:
读取所有行(readlines),并显示行号,代码如下:
import osdir_path = r'C:\Users\86182\Desktop\read_file'file_name = "test_file_01.k"file_path = os.path.join(dir_path,file_name)try:    with open(file_path,"r",encoding="utf-8") as f:        lines = f.readlines()for line_num,content in enumerate(lines,start=1):            clean_content = content.rstrip('\n')print(f"{line_num}:{clean_content}")except Exception as e:print(f"open failed due to {e}")
运行代码后输出如下:
enumerate() 的作用是给可迭代对象(如列表、字符串、文件行等)的每个元素配上一个序号,遍历的时候能同时拿到 “序号” 和 “元素本身”。
enumerate(iterable, start=0)
interable:必须参数,要遍历的可迭代对象(列表,元组,字符串等)。
start:可选参数,序号的起始值,默认是0.
rstrip()
Python字符串的内置方法,作用是移除字符串末尾(右侧)的指定字符,如果不指定字符,默认移除空白字符(包括换行符\n,空格,制表符\t,回车符\r)。
方法
作用
rstrip()
移除字符串右侧指定字符
lstrip()
移除字符串左侧指定字符
strip()
移除字符串两端指定字符

最新文章

随机文章