Python 接收到内存错误?

Python 接收到内存错误?,python,file,memory,python-3.x,directory,Python,File,Memory,Python 3.x,Directory,我在学院一位教授的办公室工作,他让我通读一整堂课的论文,试图抓住剽窃者,所以我决定用python编写一个程序,查看所有论文中的六个单词短语,并对它们进行比较,看看是否有任何一篇论文有超过200个匹配的短语。例如,六个单词的短语是 我吃了一个土豆,很好吃。将是: 我吃了一个土豆,吃了它 吃了一个土豆,它是 一个土豆,很好吃 我的代码目前是 import re import glob import os def ReadFile(Filename): try: F = op

我在学院一位教授的办公室工作,他让我通读一整堂课的论文,试图抓住剽窃者,所以我决定用python编写一个程序,查看所有论文中的六个单词短语,并对它们进行比较,看看是否有任何一篇论文有超过200个匹配的短语。例如,六个单词的短语是

我吃了一个土豆,很好吃。将是:

我吃了一个土豆,吃了它

吃了一个土豆,它是

一个土豆,很好吃

我的代码目前是

import re
import glob
import os

def ReadFile(Filename):
    try:
        F = open(Filename)
        F2=F.read()
    except IOError:
        print("Can't open file:",Filename)
        return []
    F3=re.sub("[^a-z ]","",F2.lower())
    return F3
def listEm(BigString):
    list1=[]
    list1.extend(BigString.split(' '))
    return list1

Name = input ('Name of folder? ')
Name2=[]
Name3=os.chdir("Documents")
for file in glob.glob("*txt"):
    Name2.append(file)

for file in Name2:
    index1=0
    index2=6
    new_list=[]
    Words = ReadFile(file)
    Words2= listEm(Words)
    while index2 <= len(Words2):
        new_list.append(Words2[index1:index2])
        index1 += 1
        index2 += 1

    del Name2[0]  ##Deletes first file from list of files so program wont compare the same file to itself.

    for file2 in Name2:
        index=0
        index1=6
        new_list2=[]
        Words1= ReadFile(file2)
        Words3= listEm(Words)
        while index1 <= len(Words3):
            new_list2.append(Words3[index:index1])  ##memory error
            index+=1
            index2+=1
    results=[]
    for element in new_list:
        if element in new_list2:
            results.append(element)
    if len(results) >= 200:
        print("You may want to examine the following files:",file1,"and",file2)

由于某种原因,我不知道我做错了什么,在我短短的一个学期的编程生涯中,我从来没有收到过一个记忆错误。感谢您的帮助。

您可能希望在
中增加
index1
而不是
index2
,同时
出现错误。将
index2+=1
更改为
index1+=1

由于
index1,当前处于无限循环中
new_list2.append(Words3[index:index1])