Python Error message when opening .txt file / change in working directory
<p>Iâm a new python user and have written a python script that prompts for the name of a text file (.txt) to be opened and read by the program. </p>

<pre><code>name = raw_input("Enter file:")
if len(name) &lt; 1: 
    name = "test.txt"
handle = open(name)
</code></pre>

<p>This was working perfectly fine for me, but then I installed Anaconda and tried to run the same <code>.py</code> script and kept getting this error message:</p>

<pre><code>$ python /test123.py
Enter file:'test.txt'
Traceback (most recent call last): 
  File "/test123.py", line 26, in &lt;module&gt;
    handle = open(name)
IOError: [Errno 2] No such file or directory: "'test.txt'"
</code></pre>

<p>even though I had changed nothing in the script or text file and the text file definitely exists in the directory.  So then I uninstalled Anaconda and deleted the Anaconda directory as well as the PATH variable pointing to Anaconda in the <code>~/.bash_profile</code> file to see if that would fix the problem.  But Iâm still getting the same error :(  Whatâs wrong?  I do not get any error if I run the file in the Terminal directly from TextWrangler, and my <code>.py</code> script contains the shebang line <code>#!/usr/bin/env python</code></p>

<p>My info:</p>

<pre><code>$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.11.6
BuildVersion:   15G1004

$ python
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 26 2016, 12:10:39) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin

$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python

$ /usr/bin/python -V
Python 2.7.10
</code></pre>

<p>Thanks so much for the time and help!</p>

<p><strong>EDIT:</strong>
I copied both the <code>test123.py</code> document and the <code>test.txt</code> file and put them in the directory <code>/Users/myusername/Documents</code> and ran the script again, this time inputting <code>/Users/myusername/Documents/test.txt</code> (without quotes) at the "Enter file" prompt, and it worked fine!  So now I need to modify my question:</p>

<p>My understanding--and what had been working fine for me previously--is that when a python script is run, the working directory automatically changes to the directory that the <code>.py</code> file is inside.  Is this not correct?  Or how could I change my settings back to that, since somehow it seemed to have been the case before but now isn't.</p>

<p><strong>EDIT 2:</strong>
Now that I've realized the source of my original error was a result of the working directory Python was using, I've been able to solve my problem by adding the following lines to my <code>test123.py</code> script:</p>

<pre><code>import os
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
</code></pre>

<p>as suggested here: <a href="http://stackoverflow.com/questions/1432924/python-change-the-scripts-working-directory-to-the-scripts-own-directory">python: Change the scripts working directory to the script&#39;s own directory</a></p>

<p>However, I'm also selecting the answer provided by @SomethingSomething because now that I've been able to change the working directory, the input <code>'test.txt'</code> (with quotes) is not valid, but <code>test.txt</code> (without quotes) works as desired.</p>

<p>(The only thing I'm still wondering about is why previously, when I was running python scripts, the working directory automatically changed to the directory that the <code>.py</code> file was inside, and then somehow the setting changed, corresponding with my installation of Anaconda.  However, now that I've solved my problem this seems unimportant.)</p>

<p>Thanks everyone for the help!</p>

<p>It's very simple, your input is wrong:</p>

<p>Incorrect:</p>

<pre><code>$ python /test123.py
Enter file:'test.txt'
</code></pre>

<p>Correct:</p>

<pre><code>$ python /test123.py
Enter file:test.txt
</code></pre>

<p>do not include the <code>'</code> characters. It is not code, it's just standard input</p>

python python-2.7