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>First of all, I recommend you to use relative path, not absolute path. It is simpler and will make your life easier especially now that you just started to learn Python. If you know how to deal with commandline, run a new commandline and move to the directory where your source code is located. Make a new text file there and now you can do something like</p>

<pre><code> f = open("myfile.txt")
</code></pre>

<p><br/></p>

<p>Your error indicates that there is something wrong with path you passed to builtin function <b>open</b>. Try this in an interactive mode,</p>

<pre><code>&gt;&gt; import os
&gt;&gt; os.path.exists("C:/Users/****/AppData/Local/Programs/Python/Python35-32/lib/book.txt")
</code></pre>

<p>If this returns False, there is nothing wrong with your <b>getText</b> fuction. Just pass a correct path to <b>open</b> function.</p>

python error-handling