Stuck at reading between the lines


#1




Compiler:
Traceback (most recent call last):
File "python", line 1, in
IOError: [Errno 2] No such file or directory: 'text.txt'
codeacadamy:
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()
my_file.close()


#2

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.


#3

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


#4

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,


#5

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


#6

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:


#7

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.


#8

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


#9

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