Bug in Lesson 5 or am I missing something?


#1

This Code:

my_file = open("text.txt", "r")
print my_file.readline()
print my_file.readline()
print my_file.readline()
my_file.close()

Gives me this error:

Traceback (most recent call last):
  File "python", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'text.txt'

But the instruction tells me

calling open() on the "text.txt" file in "r"ead-only mode

#2

i have the same problem


#3

As another thread said, make a small edit in the "text.txt" file (which will force it to be saved again), then hit the "Save & Submit Code" button to re-run your solution.


#4

That did not worked for me. I solved it by first using python to create that text.txt file with the 3 lines, before I read from thjat file:

new_file = open('text.txt', "w")
new_file.write("I'm the first line of the file!\n")
new_file.write("I'm the second line.\n")
new_file.write("Third line here, boss.\n")
new_file.close()
my_file = open('text.txt', "r+")

print my_file.readline()
print my_file.readline()
print my_file.readline()

my_file.close()

#5

I added another random word as fourth line on "text.txt" tab and added a fourth "print my_file.readline()" on main tab and hit submit. In my case, only this fixed the bug.


#6

Thanks, this works, bug fixed


#7

thanks peshay..it did work..so we have to make this file first, write 3 line inside and then read it ..good work :grin:


#8

As the rest said its a bug. Best thing to do is to go into the "text.txt" file tab beside the script.py tab on the top of the editor interface on codeacademy and put a space at the end of the 3rd line. I did that, then rewrote to file (r+ for read/write instead of a for append) and got success with the following code:

my_file = open('text.txt', "r+")
my_file.write("Hello file not working.\n")
my_file.write("Its 5 years as a bug.\n")
my_file.write("And another 5 to its fix.\n")
my_file.close()

my_file = open("text.txt", "r")
print (my_file.readline())
print (my_file.readline())
print (my_file.readline())
my_file.close()

I was successful ONLY after I spaced the 3rd line of the text.txt file by jumping into it at the tabs on top of the editor interface where you have script.py | text.txt.


#9

Its help me!
:+1:Thanks


#10

this bug still exist!


#11

yep! that worked (:smile:


#12

This is quite annoying :rage:
I spent so much time thinking I was the one with the problem. Please, fix the bug.


#13

me too. it's the site i think


#14

work for me, i add 1 to text file, then hit run, again i went to code, and it succeed


#15

I don't know why but for some reason this code works. THank You !!


#16

Are there any plans to fixing this bug??


#17

Nice it worked a charm :slight_smile: