Blockquote
FileNotFoundError: [Errno 2] No such file or directory: 'ex15_sample.txt ’
I have tried more than a dozen times, it failed constantly! I don’t know where did i made a mistake.
Blockquote
FileNotFoundError: [Errno 2] No such file or directory: 'ex15_sample.txt ’
I have tried more than a dozen times, it failed constantly! I don’t know where did i made a mistake.
from sys import argv
script, filename = argv
txt = open(filename)
print(f"Here’s your file {filename}:")
print(txt.read())
print(“Type the filename again:”)
file_again = input("> ")
txt_again = open(file_again)
print(txt_again.read())
it’s driving me crazy as well. did you figure out the solution
Guys, there’s the seminar today in 1 hour, so you can ask Zed directly about problems with exercises from LPTHW book.
https://forum.learncodethehardway.com/t/python-basics-workshop-review-ex11-20-wed-3pm-est/2182
In your screenshot, on the error line, it looks as if there is a space between the ex15_sample.txt and the final quote mark. I was only able to reproduce this error if I pressed the space bar before I hit the return key.
THIS is when I press the space bar after typing in ex15_sample.txt
Here's your file ex15_sample.txt:
Again testing.
For Python.
For learning.
Type the filename again:
> ex15_sample.txt
Traceback (most recent call last):
File "ex15.py", line 13, in <module>
txt_again = open(file_again)
FileNotFoundError: [Errno 2] No such file or directory: 'ex15_sample.txt '
THIS time I’ll enter an incorrect filename but without the space:
Here's your file ex15_sample.txt:
Again testing.
For Python.
For learning.
Type the filename again:
> ex14_sample.txt
Traceback (most recent call last):
File "ex15.py", line 13, in <module>
txt_again = open(file_again)
FileNotFoundError: [Errno 2] No such file or directory: 'ex14_sample.txt'
Can you see the difference on the error lines? between 'ex15_sample.txt ’ and ‘ex14_sample.txt’
Python sees that space as part of the filename. If I don’t press the space bar after typing ex15_sample.txt, it runs OK.
Yes, that extra space character at the end is what’s causing the problem, although…hmmm yeah I don’t know if I like that as a possible error. One way to fix it is this:
file_again = input("> ").strip()
Which will remove any extra space characters. The other way is to…just stop hitting the space bar after the file name.
I’m on Windows OS. When I typed the argument into the terminal (e.g. python ex15.py ex15_sample.txt) and hit return, I would get the same error message as you. I Did some research, and it turns out the error was the file name; I named the file ex15_sample.txt, I wrote the file on the notepad app and by default notepad files save as .txt files, so the actual file name as i saved it was: “ex15_sample.txt.txt”. To fix this error I renamed the file to: “ex15_sample”.
Hope this helps.
If the user does not pass the full path to the file (on Unix type systems this means a path that starts with a slash), the python file path is interpreted relatively to the current working directory. The current working directory usually is the directory in which you started the program. In order to make this work, the directory containing the python executable must be in the PATH, a so-called environment variable that contains directories that are automatically used for searching executables when you enter a command.
In any case, if your Python script file and your data input file are not in the same directory, you always have to specify either a relative path between them or you have to use an absolute path for one of them.