Python: How to fix an [errno 2] when trying to open a text file?
<p>I'm trying to learn Python in my free time, and my textbook is not covering anything about my error, so I must've messed up badly somewhere. When I try to open and read a text file through notepad (on Windows) with my code, it produces the error. My code is:</p>

<pre><code>def getText():
    infile = open("C:/Users/****/AppData/Local/Programs/Python/Python35-32/lib/book.txt" , "r")
    allText = infile.read()
    return allText
</code></pre>

<p>If it's necessary, here is the rest of my code so far:</p>

<pre><code>def inspectWord(theWord,wList,fList):
    tempWord = theWord.rstrip("\"\'.,`;:-!")
    tempWord = tempWord.lstrip("\"\'.,`;:-!")
    tempWord = tempWord.lower()
    if tempWord in wList:
        tIndex = wList.index(tempWord)
        fList[tIndex]+=1
    else:
        wList.append(tempWord)
        fList.append(1)

def main():
     myText = getText()
     print(myText)

main()
</code></pre>

<p>I'd greatly appreciate any advice, etc.; I cannot find any help for this. Thanks to anyone who responds.</p>

<p>To open a unicode file, you can do the following</p>

<pre><code>import codecs

def getText():
    with codecs.open("C:/Users/****/AppData/Local/Programs/Python/Python35-32/lib/book.txt" , "r", encoding='utf8') as infile:
        allText = infile.read()
    return allText
</code></pre>

<p>See also:  <a href="http://stackoverflow.com/questions/147741/character-reading-from-file-in-python">Character reading from file in Python</a></p>

python error-handling