从多个文件读取时出现内存错误

memory error when reading from multiple files

我正在阅读位于桌面文件夹中的 15 个文件。我可以毫无问题地读取前几个文件,但是当我深入了解它时,我最终遇到了一个内存错误,该错误看起来是由迭代器中内置的 Python 引起的。请注意,我使用多个列表来保存所有数据,因为我使用的是 32 位 python,并且由于文件太大,它们很快就会填满。

x=0 #represents the file being read
for file in glob.glob(path): #reads all files in a folder
    with open(file) as f: 
        print (f)
        for line in f:    
            if "jack" in line: 

                if(x<=3):#prevent memory error
                    jack1.append(line)

                elif(x>3 and x<=6):
                    jack2.append(line)

                elif(x>6 and x<=8):
                    jack3.append(line)

                elif(x>8 and x<=9):#prevent memory error with try except 
                    try:
                        jack4.append(line)

                    except:
                        jack5.append(line)

Traceback (most recent call last):
  File "C:\Users\erik.kniaz\workspace\arif help\jack.py", line 102, in <module>
for line in f:    
MemoryError

I am using 32bit python

这是你的问题。 32 位进程,每个 generally speaking, are limited to 4 GB 虚拟内存,减去内核 space,这可能很重要。您将需要切换到 64 位计算或重新设计您的程序以消耗更少的内存。这通常是通过将数据推回文件系统而不是将其保存在内存中来完成的。