首页 > 试题广场 >

在Python3中,要读取一个非常大的文件,以下哪个方法可以

[单选题]
在Python3中,要读取一个非常大的文件,以下哪个方法可以避免一次性将整个文件读入内存?
  • read()
  • append()
  • readlines()
  • 迭代器
不对吧,read(n)也是读取n个字符,不过read()确实是读完
chunk_size = 4096  # 每次读取的字节数
with open("large_file.bin", "rb") as file:
    while True:
        chunk = file.read(chunk_size)  # 按块读取
        if not chunk:
            break
        process(chunk)  # 处理每一块

发表于 2025-07-21 11:20:54 回复(0)