我有.txt
个文件,每个文件都包含一个很长的行,程序停在b.append(int(j))
行,导致内存错误。我不明白为什么程序出现内存错误,因为文件大小为2.8 MB,RAM大小为28GB。
if __name__=='__main__':
path=raw_input("enter file path:")
image_path=raw_input("enter directory where images are to be stored:")
count = 0
for f in sorted(os.listdir(path)):
print(f)
file = path+'/'+f
b = []
a =[]
ref =[]
alt = []
if (f[:-4] == '0'):
print('cb1')
ch = open(file,'r')
for i in ch:
b += i.split()
for j in b:
b.append(int(j))
count+=1
print('cb2')
elif (f[:-4] == '1'):
po = open(file,'r')
for i in po:
a += i.split()
for j in a:
a.append(int(j))
count+=1
elif (f[:-4] == '3'):
re = open(file,'r')
for i in re:
ref += i.split()
count+=1
elif (f[:-4] == '4'):
al = open(file,'r')
for i in al:
alt += i.split()
count+=1
if (count == 4):
break
cnt = 0
for f in sorted(os.listdir(path)):
print(f)
file = path+'/'+f
cnt += 1
if cnt>4:
process(path+'/'+f, image_path , f)
ch.close()
po.close()
re.close()
al.close()
我在ubuntu 16.04上使用python 2.7 64bit,内存为28 GB。可以从here
下载0.txt答案 0 :(得分:5)
你有:
for j in b:
b.append(int(j))
您正在迭代它时将项目附加到b
。这样你的列表就会无限增长。
你可能想要做一些像b = [int(j) for j in b]
这样的事情。