python读取大文件时避免内存溢出


大文件直接用read()函数会内存溢出

解决办法

1. 一行一行的取

with open('t1.txt') as f:
    while True:
        data = f.readline()
        # 判断文件是否结束
        if not data:
            break

2. 使用第三方模块linecache,可以取指定行

import linecache

# 读取第2行
data = linecache.getline('t1.txt',2)

参考

https://blog.csdn.net/weixin_40006779/article/details/109879397