Stuck at reading between the lines


Traceback (most recent call last):
File "python", line 1, in
IOError: [Errno 2] No such file or directory: 'text.txt'
Oops, try again. Sorry, but I'm gonna need you to refresh the page! Don't worry your code is saved.

i refreshed the page 10 15 times but it gives the same response.

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


Added a 4th line to the text.txt file as follows:
I'm the first line of the file!
I'm the second line.
Third line here, boss.
I am the fourth.

Worked like a charm.


so you got it to work? For everyone else stuck here, try mac42 solution or this one:

write to the file with python before trying to read from the file


Hi everybody!!

I had exactly the same issue and Mac42's solution (write a 4th line into "text.txt") worked!!
I don't understand why though.

Best Regards,


i would seem, adding a 4th line to the file causes the file to save or something.


not sure about the issue but adding the 4th line basically makes the compiler recognize the text.txt file and it seems to be working. :slight_smile:


If refreshing doesn't work, you can add the multi-line string code (three single quotes ' ' ') to the text.txt file and that should help. It was the only thing I found that worked.


Just do it in r+ mode instead of r mode.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.