This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by davidqx
about 10 years

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

6 votes

Permalink

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()
points
over 8 years

Answer 556088d6e39efe3f210006f9

1 vote

Permalink

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()

points
Submitted by Daniel Abeles
almost 9 years

5 comments

CodeBoy almost 9 years

It’s really help, thanks a lot

Daniel Abeles almost 9 years

Glad to Help :)

Thanks so much! Don’t forget to close the file though, myfile.close()

Daniel Abeles almost 9 years

#hlucian Thanks just added that last line.

scotty511 over 8 years

Thanks, this worked after I changed the ‘f’s to ‘my_file’s

Answer 5588c8d6937676878e00012d

1 vote

Permalink

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?

points
Submitted by TaeHee Kim
almost 9 years

Answer 559ba8e8e39efe38810005d3

1 vote

Permalink

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()
points
almost 9 years

1 comments

dbgaulton almost 9 years

Thanks man! That one finally worked for me.

Answer 55faf9aed3292f5ec40002ce

1 vote

Permalink

hi, just type anything on text.txt file and run the code or refresh page

points
Submitted by amiah100
over 8 years

Answer 52dc89eb80ff338179007e99

0 votes

Permalink

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.

points
Submitted by davidqx
about 10 years

4 comments

RfDjalma about 10 years

you correction dosent work for me…

RfDjalma about 10 years

but, f5 correct the problem.

ftimh about 10 years

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()

ftimh about 10 years

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

0 votes

Permalink

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).

points
Submitted by Sidleg
about 10 years

2 comments

Diego Esquinazi about 10 years

Also works for me

willman32 about 10 years

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

0 votes

Permalink

I got the same thing, @RfDjalma was right, refresh your webpage will fix this problem.

points
Submitted by void main Anderson
about 10 years

3 comments

lc about 10 years

Refreshing a couple times worked for me.

Zanetti over 9 years

Yes, that is! Refresh your webpage and try again.

Andres Valdes over 9 years

Thank you!

Answer 52fa4fed8c1ccc63090006b8

0 votes

Permalink

davidqx I had the same exact problem. Solved it using the method used by Sidleg. So…Thanks Sidleg!

points
Submitted by Uricorn
about 10 years

Answer 53cd027c282ae3216c001a67

0 votes

Permalink

my_file = open("output.txt", "r")
print my_file.read()`
my_file.close()
points
over 9 years

Answer 542a2a8e8c1ccc003600018b

0 votes

Permalink

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()

points
Submitted by kihoonhan
over 9 years

1 comments

Joe Yousef over 8 years

it worked for me but why XD

Answer 55efab5395e3784d910002f4

0 votes

Permalink

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?

points
Submitted by vpustell
over 8 years