如代码所示,可能有的人会读取不到words.txt或者加载的数字不对,我google了两个小时,以为是什么特别厉害的错误,结果只是因为绝对路径错了~
def loadWords():
"""
Returns a list of valid words. Words are strings of lowercase letters.
Depending on the size of the word list, this function may
take a while to finish.
"""
print "Loading word list from file..."
# inFile: file
inFile = open(WORDLIST_FILENAME, 'r', 0) # better use complete pathname!!!...
# wordList: list of strings
wordList = []
for line in inFile:
wordList.append(line.strip().lower())
print " ", len(wordList), "words loaded."
return wordList
上方的WORDLIST_FILENAME = "....../words.txt"
改成绝对路径就可以了~