苦练Python第67天:光速读取任意行,linecache模块解锁文件处理新姿势

216 阅读4分钟

前言

大家好,我是 倔强青铜三。欢迎关注我,微信公众号: 倔强青铜三。点赞、收藏、关注,一键三连!

今天咱们把 Python 自带的“光速行读取器”—— linecache 模块,从开箱到实战一次性讲透。


一、为什么需要 linecache?

  • 秒级随意读任意行:再也不用 for i, line in enumerate(f) 数行号。
  • 内存缓存机制:同一个文件多次读取,只加载一次。
  • 源码级调试神器tracebackpdb 都在用它。
  • 零依赖:官方出品,随 Python 一起安装。

linecache 常用 API 一览表

API作用关键入参返回值
linecache.getline(filename, lineno)读取指定行filename 文件路径;lineno 从 1 开始字符串(末尾带 \n 或空串)
linecache.clearcache()清空全部缓存
linecache.checkcache(filename=None)检查文件是否更新filename=None 时检查所有无,更新缓存
linecache.lazycache(filename, module_globals)惰性缓存(3.5+)高级调试场景

二、30 秒完成首行读取

# demo_getline.py
import linecache

# 先造个测试文件
with open("poem.txt", "w", encoding="utf-8") as f:
    f.write("白日依山尽\n黄河入海流\n欲穷千里目\n更上一层楼\n")

line = linecache.getline("poem.txt", 2)
print("第2行内容:", repr(line))

运行效果:

2行内容: '黄河入海流\n'

三、读取不存在的行会怎样?

# demo_out_of_range.py
import linecache

# 文件只有4行,读第10行
line = linecache.getline("poem.txt", 10)
print("第10行内容:", repr(line))
print("内容长度:", len(line))

运行效果:

第10行内容: ''
内容长度: 0

四、缓存机制验证:读同一文件十万次

# demo_cache.py
import linecache
import time

def read_100k():
    start = time.perf_counter()
    for _ in range(100000):
        linecache.getline("poem.txt", 3)
    cost = time.perf_counter() - start
    print("10万次读取耗时:{:.4f}s".format(cost))

read_100k()

运行效果(示例,因机器差异浮动):

10万次读取耗时:0.0362s

五、清空缓存再测速度

# demo_clear.py
import linecache
import time

linecache.clearcache()  # 清空缓存

start = time.perf_counter()
for _ in range(1000):
    linecache.getline("poem.txt", 3)
cost = time.perf_counter() - start
print("清空缓存后1000次读取耗时:{:.4f}s".format(cost))

运行效果:

清空缓存后1000次读取耗时:0.0012s

六、文件更新后如何刷新缓存?

# demo_checkcache.py
import linecache
import time

# 首次读取
print("原第1行:", repr(linecache.getline("poem.txt", 1)))

# 模拟文件被外部修改
with open("poem.txt", "w", encoding="utf-8") as f:
    f.write("新内容覆盖第一行\n")

# 无 checkcache,读到的仍是旧缓存
print("未刷新缓存时:", repr(linecache.getline("poem.txt", 1)))

# 刷新缓存
linecache.checkcache("poem.txt")
print("已刷新缓存时:", repr(linecache.getline("poem.txt", 1)))

运行效果:

原第1行: '白日依山尽\n'
未刷新缓存时: '白日依山尽\n'
已刷新缓存时: '新内容覆盖第一行\n'

七、读取超大文件第 1 万行

# demo_bigfile.py
import linecache
import os

big = "big.txt"
# 生成 1 万行
with open(big, "w") as f:
    for i in range(10000):
        f.write(f"line {i+1}\n")

line = linecache.getline(big, 10000)
print("第10000行:", repr(line))

运行效果:

10000行: 'line 10000\n'

八、读取源码:查看 linecache模块 自己的第 42 行

# demo_inspect.py
import linecache
import linecache as lc  # 模块本身也是文件

line42 = lc.getline(lc.__file__, 42)
print("linecache.py 第42行:", repr(line42))

运行效果(不同版本略有差异,此为Python 3.13.3):

linecache.py42行: '        return updatecache(filename, module_globals)\n'

九、一行代码实现简易 grep

# demo_grep.py
import linecache
import sys
import os

def grep(filename, keyword):
    if not os.path.isfile(filename):
        print("文件不存在")
        return
    lineno = 1
    while True:
        line = linecache.getline(filename, lineno)
        if not line:
            break
        if keyword in line:
            print(f"{lineno}:{line.rstrip()}")
        lineno += 1

# 测试
with open("log.txt", "w") as f:
    f.write("INFO start\nDEBUG config\nINFO end\n")

grep("log.txt", "INFO")

运行效果:

1:INFO start
3:INFO end

十、完整实战:日志行级随机抽查器

功能:随机抽查 5 行日志,显示行号与内容。

# log_sampler.py
import linecache
import random
import os

logfile = "server.log"
# 生成模拟日志
with open(logfile, "w") as f:
    for i in range(1, 2001):
        f.write(f"[{i}] 200 GET /api/data/{i}\n")

total_lines = sum(1 for _ in open(logfile, "rb"))
sample = random.sample(range(1, total_lines + 1), 5)

print("随机抽查的5行:")
for no in sample:
    print(f"{no:04d}: {linecache.getline(logfile, no).rstrip()}")

运行效果(示例):

随机抽查的5行:
0187: [0187] 200 GET /api/data/187
0999: [0999] 200 GET /api/data/999
1234: [1234] 200 GET /api/data/1234
0003: [0003] 200 GET /api/data/3
1500: [1500] 200 GET /api/data/1500

小结

武器用途一句话记忆
getline读任意行行号从1开始
clearcache清缓存内存不膨胀
checkcache检更新文件改后必刷新
零依赖官方自带开箱即用

如果这篇文章帮到了你,欢迎请我喝一杯咖啡☕️,点击下面的【喜欢作者】按钮 进行打赏,让我继续熬夜码字!

最后感谢阅读!欢迎关注我,微信公众号: 倔强青铜三
欢迎 点赞、收藏、关注,一键三连!!