Mabyn Mabyn - 1 year ago 73
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
script and kept getting this error message:

$ python /
Enter file:'test.txt'
Traceback (most recent call last):
File "/", 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
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
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

$ /usr/bin/python -V
Python 2.7.10

Thanks so much for the time and help!

I copied both the
document and the
file and put them in the directory
and ran the script again, this time inputting
(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
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.

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

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

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
(with quotes) is not valid, but
(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
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 Source

It's very simple, your input is wrong:


$ python /
Enter file:'test.txt'


$ python /
Enter file:test.txt

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download