This forum is now read-only. Please use our new forums! Go to forums
Lesson "Reading Between the Lines": No such file or directory: 'text.txt'
The instructions say, “Open the text.txt file in “r”ead-only mode and set this equal to the file object my_file.” My code does that:
my_file = open("**text.txt**", "r")
However, I get these errors:
Traceback (most recent call last):
File "python", line 1, in <module>
IOError: [Errno 2] No such file or directory: '**text.txt**'
And
Oops, try again. Make sure you’re trying to open text.txt, not output.txt!
Here’s my code:
my_file = open("text.txt", "r")
print(my_file.readline())
print(my_file.readline())
print(my_file.readline())
my_file.close()
Answer 55dab4109113cbbf0900013f
Problem is still there. My workaround is pretty straightforward - if you have no file, create one!:
file = open("text.txt", "w")
file.write("I'm the first line of the file!\n")
file.write("I'm the second line.\n")
file.write("Third line here, boss.\n")
file.close()
my_file = open("text.txt", "r")
print my_file.readline()
print my_file.readline()
print my_file.readline()
my_file.close()
Answer 556088d6e39efe3f210006f9
Managed to make a workaround:
with open(“text.txt”, “w”) as f: f.write(“hello”)
my_file = open(“text.txt”, “r”) print my_file.readline() print my_file.readline() print my_file.readline()
my_file.close()
Answer 5588c8d6937676878e00012d
I encountered the same problem.
my_file = open("text.txt", "r")
print my_file.readline()
print my_file.readline()
print my_file.readline()
my_file.close()
Traceback (most recent call last):
File "python", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'text.txt'
Is there a solution?
Answer 559ba8e8e39efe38810005d3
Don’t you try str() method? (I solved it so)
my_file = open("text.txt", "r")
print(str(my_file.readline()))
print(str(my_file.readline()))
print(str(my_file.readline()))
my_file.close()
1 comments
Thanks man! That one finally worked for me.
Answer 55faf9aed3292f5ec40002ce
Answer 52dc89eb80ff338179007e99
This is really bad. The error message is totally misleading. Here’s how I had to write my code to make it pass:
my_file = open("text.txt", "r")
print my_file.readline()
print my_file.readline()
print my_file.readline()
my_file.close()
That sucks.
4 comments
you correction dosent work for me…
but, f5 correct the problem.
I rewrite this code like the following, and it works. my_file = open(“output.txt”, “r”) print my_file.readline() print my_file.readline() print my_file.readline() my_file.close()
I rewrite this code like the following, and it works. my_file = open(“output.txt”, “r”) print my_file.readline() print my_file.readline() print my_file.readline() my_file.close()
Answer 52fa0b8a80ff33e740001c52
I had this same problem. I solved it by changing the file to exactly what I was supposed to be not using!
my_file = open(“output.txt”, “r”)
!
It didn’t print the correct file but at least I was able to progress (even though I shouldn’t have been able to).
2 comments
Also works for me
Works for me, this last section has me spending more time of trying to get around Codeacademy’s checking tool than to actually learn the code. Thank god this is the last section for python, it seems that there is a bug in every lesson.
Answer 52fa313e52f863548e0024bd
I got the same thing, @RfDjalma was right, refresh your webpage will fix this problem.
3 comments
Refreshing a couple times worked for me.
Yes, that is! Refresh your webpage and try again.
Thank you!
Answer 52fa4fed8c1ccc63090006b8
Answer 53cd027c282ae3216c001a67
my_file = open("output.txt", "r")
print my_file.read()`
my_file.close()
Answer 542a2a8e8c1ccc003600018b
how about this code?
my_file = open(“./text.txt”, “r”) print my_file.readline() print my_file.readline() print my_file.readline() my_file.close()
1 comments
it worked for me but why XD
Answer 55efab5395e3784d910002f4
For fuck’s sake. I’ve tried every single solution offered here, AND refreshed the page well over 50 times over the course of 2 hours… STILL won’t work.
Why is it that I spend more time debugging Codecademy’s shitty lesson writing than actually learning Python?
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
5 comments
It’s really help, thanks a lot
Glad to Help :)
Thanks so much! Don’t forget to close the file though, myfile.close()
#hlucian Thanks just added that last line.
Thanks, this worked after I changed the ‘f’s to ‘my_file’s