Reading Between the Lines


#1




my_file = open("text.txt", "r")
print (m_file.readlines())

my_file.close()

Can someone tell me what is the problem here ? When i refresh the page , i get the same error, if i try to change a little the code i get errors like ( you dont get first string) ...


#2

ok , found the solotion !

Frist we need to make a file :

my_file = open("text.txt", "w")

Then the rest if code )


#3

wow man that's creative!!


#4

ya i got the same thing when submitting my code. it was different than your original one but still should have worked properly:

my_file = open("text.txt", "r")

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

my_file.close()

why does your code work, yet mine doesn't? just curious. codecademy should fix this bug..


#5

Hi ,

I went back on some of the completed codes and get the same error , is it a generic issue ?


#6

i was also facing the same problem but i put 'r' instead of "r" in first line.. and it worked.


#7

First write the file and then read the file.

my_file = open("text.txt", "w")
my_file.write("I'm the first line of the file!\n")
my_file.write("I'm the second line.\n")
my_file.write("Third line here, boss.\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()


#8

But, why do we need to write the file first? The file is already given and it has the text written into it. We may have to open and read an existing file. how will it work then??


#9

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