当尝试使用whoosh时,我得到这个错误“IndexError:list index out of range”?
回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我有一个名为“pads”的文件夹,我在其中创建了6个记事本文档(1.txt,2.txt..等等..6.txt),我试图执行下面的代码,并得到以下错误</p>
<pre><code>import os
from whoosh.index import create_in
from whoosh.fields import Schema, TEXT, ID
import sys
from whoosh.qparser import QueryParser
from whoosh import scoring
from whoosh.index import open_dir
def createSearchableData(root):
'''
Schema definition: title(name of file), path(as ID), content(indexed but not stored),textdata (stored text content)
'''
schema = Schema(title=TEXT(stored=True),path=ID(stored=True),\
content=TEXT,textdata=TEXT(stored=True))
if not os.path.exists("indexdir"):
os.mkdir("indexdir")
# Creating a index writer to add document as per schema
ix = create_in("indexdir",schema)
writer = ix.writer()
filepaths = [os.path.join(root,i) for i in os.listdir(root)]
for path in filepaths:
fp = open(path,'r')
print(path)
text = fp.read()
writer.add_document(title=path.split("\\")[1], path=path,\
content=text,textdata=text)
fp.close()
writer.commit()
root = "pads"
createSearchableData(root)
</code></pre>
<pre><code>###ERROR###
pads/5.txt
IndexError: list index out of range
</code></pre>
<p>为什么它要读取一个记事本文档,它是5.txt文件,而不是其他文件?你知道吗</p>