1

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!

Community
  • 1
  • 1
Mabyn
  • 316
  • 2
  • 20
  • 1
    Looks like the file may not be in your working directory Anaconda is using. In Anaconda try `import os` `os.getcwd()` to check, I think there's a menu option to modify your working directory, or you could move the file. – Andrew Sep 11 '16 at 17:58
  • 1
    remove the quote characters when you input the string in STDIN !!! – SomethingSomething Sep 11 '16 at 18:02
  • Without the quotes I get the same error message unfortunately. – Mabyn Sep 12 '16 at 04:08
  • @Andrew I've already uninstalled Anaconda. I put both the test123.py file and the test.txt file in my home directory. – Mabyn Sep 12 '16 at 04:12
  • See my edit above in the original post – Mabyn Sep 12 '16 at 16:43

1 Answers1

3

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

SomethingSomething
  • 11,491
  • 17
  • 68
  • 126
  • I get the same error message with or without the quotes :-/ When I run the script in the terminal directly from the text editor I'm using (TextWrangler) it actually works fine with or without the quotes. – Mabyn Sep 12 '16 at 04:07
  • Thanks! This was, apparently, one of two of my problems. See my edit above. – Mabyn Sep 12 '16 at 17:38