Reading


#1

Hello, the bellow code seems should have to work:

my_file = open("output.txt", "r")
print my_file.read()
my_file.close()

but instead it shows the error:

Oops, try again. Did you create a variable called my_file?


#2

All You have to do is write on a few lines in the text file my_file and then your code will work and you will pass the SCT. :slight_smile:


#3

Thanks, it worked! Probably, the instruction needs to be corrected..


#4

We live, we learn. :smile:


#5

there is no file as text.txt so make a new one like this:

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

#6
my_file = open("output.txt", "r")
print my_file.read() 
my_file.close()

Congratulations, you've finished this section! Next: The Devil's in the Details→

Well @aghabeg, this code worked! I think there is a glitch on your computer!?


#7

WELL... its kinda wierd... i did it again and it gave me an error?
but it got it the first time... anyone knows why???


#8

I think i got it... cuz the programme remembers what it has read before so such things will happen!


#9

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