10+ gig文件读取问题Python

时间:2015-04-22 21:39:43

标签: python file memory

3要逐行读取文件,因此:

with open(file) as f:
    for line in f:
        print line

for line in open(file):
    print line

尝试逐行读取,但是一旦文件大小开始超过10 GB,python选择尝试将整个文件读入内存...(适用于文件大小<10 GB)

知道为什么吗?

1 个答案:

答案 0 :(得分:1)

您可以使用可选参数来限制一次可以读取的字符数:

with open(file, "r") as f:
    line = f.readline(max_chars)
    while line:
        print(line, end='')
        line = f.readline(max_chars)