Mabyn Mabyn - 2 months ago 10
Python Question

Python Error message when opening .txt file / change in working directory

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.

name = raw_input("Enter file:")
if len(name) < 1:
name = "test.txt"
handle = open(name)


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

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


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
~/.bash_profile
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
.py
script contains the shebang line
#!/usr/bin/env python


My info:

$ 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


Thanks so much for the time and help!

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

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
.py
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.

EDIT 2:
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
test123.py
script:

import os
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)


as suggested here: python: Change the scripts working directory to the script's own directory

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

(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
.py
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.)

Thanks everyone for the help!

Answer

It's very simple, your input is wrong:

Incorrect:

$ python /test123.py
Enter file:'test.txt'

Correct:

$ python /test123.py
Enter file:test.txt

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