简体   繁体   English

10多个演出文件读取问题Python

[英]10+ gig file reading problems Python

3 Files are to be read line by line, hence: 3文件将逐行读取,因此:

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

or 要么

for line in open(file):
    print line

Tried both line by line readers, but as soon as the file sizes start exceeding 10 GB python chooses to try and read the whole file into memory anyway... (works fine for file sizes <10 GB) 尝试了逐行读取器,但是一旦文件大小开始超过10 GB,python就会选择尝试将整个文件读入内存中(对于小于10 GB的文件,它工作正常)

Any idea why? 知道为什么吗?

You can use an optional parameter to limit how many characters you can read at a time: 您可以使用可选参数来限制一次可以读取多少个字符:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM