美文网首页
6.00.1x problem set 4不能读取words.t

6.00.1x problem set 4不能读取words.t

作者: HappyJoo | 来源:发表于2019-03-15 09:30 被阅读0次

如代码所示,可能有的人会读取不到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"改成绝对路径就可以了~

相关文章

网友评论

      本文标题:6.00.1x problem set 4不能读取words.t

      本文链接:https://www.haomeiwen.com/subject/fdwkmqtx.html