Python MemoryError when trying to load 5GB text file

。_饼干妹妹 提交于 2020-12-13 10:55:48

问题


I want to read data stored in text format in a 5GB file. when I try to read the content of file using this code:

file = open('../data/entries_en.txt', 'r')
data = file.readlines()

an error occurred: data = file.readlines() MemoryError My laptop has 8GB memory and at least 4GB is empty when I want to run the program. but when I monitor the system performance, when python uses about 1.5GB of memory, this error happens.
I'm using python 2.7, but if it matters please tell me solution for 2.x and 3.x What should I do to read this file?


回答1:


The best way for you to handle large files would be -

with open('../file.txt', 'r') as f:
    for line in f:
        # do stuff

readlines() would error because you are trying to load too large a file directly into the memory. The above code will automatically close your file once you are done processing on it.




回答2:


If you want to process lines in the file, you should rather use:

for line in file:
    # do something with the line

It will read the file line by line, instead of reading it all to the RAM at once.



来源:https://stackoverflow.com/questions/26032287/python-memoryerror-when-trying-to-load-5gb-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!