3要逐行读取文件,因此:
with open(file) as f:
for line in f:
print line
或
for line in open(file):
print line
尝试逐行读取,但是一旦文件大小开始超过10 GB,python选择尝试将整个文件读入内存...(适用于文件大小<10 GB)
知道为什么吗?
答案 0 :(得分:1)
您可以使用可选参数来限制一次可以读取的字符数:
with open(file, "r") as f:
line = f.readline(max_chars)
while line:
print(line, end='')
line = f.readline(max_chars)