-1

So in "Learn python the hard way", at exercise 15, you learn how to make a program open a file. Here's my code, and I typed python ex15.py ex15.txt at the command prompt. I haven't had any other problems with the program so far:

from sys import argv

script, filename = argv

txt = open(ex15.txt)

print "Here's your file: %r" % ex15.txt
print txt.read()

print "type the filename again: "
again = raw_input("> ")

again2 = open(again)
print again2.read()

and here's the error message:

Traceback (most recent call last):
  File "ex15.py", line 5, in <module>
    txt = open("ex15.txt")

IOError: [Errno 2] No such file or directory: 'ex15.txt'

I immediately suspected the problem was the file wasn't in the right place (the ex15.txt) and put it in the Python27 folder in Windows. Then after doing some internet research of the problem, I tried putting it in the working directory of cmd, and also the scripts folder in Python27, and also tried including the full pathname for the original file location (Documents), and I always get the same error message.

What am I (or my computer) missing here? the path to the script is C:\Python27, the directory of the prompt is C:\Users\Customer, and I have already stated all the locations of the text file, it is still in every one of those folders. The python program is indeed included in PATH.

SiHa
  • 7,830
  • 13
  • 34
  • 43
  • Put it in the same folder you have the script in... – Andrew Li Oct 12 '16 at 02:49
  • 1
    The source code you posted does not match the error you're getting (you use `ex15.txt` not `'ex15.txt'`). Please paste the exact source code with the full path name for the original location of the file. Note: Use `r"path\to\file"` not just `"path\to\file"` see http://stackoverflow.com/questions/2081640/what-exactly-do-u-and-r-string-flags-do-in-python-and-what-are-raw-string-l – Ella Sharakanski Oct 12 '16 at 02:54
  • lol, i copied and pasted it, no alterations. And yes, the text file is in the exact same folder as the script. –  Oct 12 '16 at 03:18
  • @thinksinbinary lol**2 that script is invalid and can't run. BTW, you can find out where the file needs to be by `import os;print os.getcwd()` (that usually works) or `import os;print os.path.abspath('.')`. – tdelaney Oct 12 '16 at 04:07
  • @tdelaney: im just doing everything exactly by the book, and running your code reveals that the path it needs to be in is the working directory for cmd, which i've already tried putting it in and is not working still. –  Oct 12 '16 at 14:21
  • Running your code results in `NameError: name 'ex15' is not defined`. Please copy and paste the code you posted into a script and test it. The error you get is on line `txt = open("ex15.txt")` _which doesn't exist in the code you posted_ but also shows that you are trying to open a hard-coded file named "ext15.txt". But your code seems to imply that you want the user to enter a filename to check. This is likely the problem BUT you need to post a working example of the problem for us to fix it. – tdelaney Oct 12 '16 at 15:09
  • if you are commenting on the quotations, the error message just adds those, and putting single and double quotes doesn't have an effect. I don't understand what you are saying, as I have posted the script exactly the way it was. What do you mean by "hard coded"? I simply created a text file in note pad named ex15.txt with text from the exercise i copied and pasted. –  Oct 12 '16 at 22:19
  • here, i fixed the problem: i just removed ".txt" from the file name only in the command prompt, and typed `python ex15.py ex15`. I tried this because i've had some with windows extension system while writing python. Strangely enough, when i run the program and it asks me to type the filename again, i must then call it ex15.txt. Strange but true, thanks for your input all. –  Oct 12 '16 at 23:26

2 Answers2

1

The file needs to be in the same folder you are running your script

If you are running atC:/myscript.py, you file needs to be at C:/ as well.

Ex:

>> cd C:
>> dir
ex15.txt  myscript.py
>> python myscript.py ex15.txt
Here's your file: 'ex15.txt'
Something that is in your text file

type the filename again: 
> ex15.txt
Something that is in your text file

Also, your code seems to be wrong. You need to use "ex15.txt", not ex15.txt without quotes. Otherwise it will be interpreted as a variable, not as a string.

See the code below:

from sys import argv

script, filename = argv

txt = open("ex15.txt")

print "Here's your file: %r" % "ex15.txt"
print txt.read()

print "type the filename again: "
again = raw_input("> ")

again2 = open(again)
print again2.read()
renno
  • 2,659
  • 2
  • 27
  • 58
  • What do mean by "running at"? If I am in `C:/Users/Whatever` I can do `python C:/myscript.py` but the file shouldn't be at `C:/`. You need to clarify whether your mean the current working directory or the path to the script. – tdelaney Oct 12 '16 at 04:09
  • Needs to be in the same path you are running the script – renno Oct 12 '16 at 12:02
  • double quotes and single quotes on ex15.txt have failed, and I even just now put the code side by side with yours and i'm still getting the same error. –  Oct 12 '16 at 14:09
  • Pay attention where you are running the script and where your file is located. They need to be under the same location! – renno Oct 12 '16 at 14:31
0

you've been reading Learn python the hard way by Zed Shaw.Don't put it as txt = open(ex15.txt) instead use : txt = open(filename). You are working with argument variables and hey are like raw_input and need to vary