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

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容