I apologize if this will sound like a basic question but I'm seeking clarification on the close method in python, I am not understanding why after closing the file I can still print the variable that points to the file
if I run the below:
poem1 = open('poem1.txt', 'r') poem_line1 = poem1.readline() poem_line2 = poem1.readline() poem_line3 = poem1.readline() print(poem_line1 + poem_line2 + poem_line3) poem1.close() print(poem1.readline()) I will get the correct message:
"ValueError: I/O operation on closed file."
but if I replace the last line directly printing the file, there is no error:
poem_line1 = poem1.readline() poem_line2 = poem1.readline() poem_line3 = poem1.readline() print(poem_line1 + poem_line2 + poem_line3) poem1.close() print (poem_line1 + poem_line2 + poem_line3)
readlineagain should let you print the fourth line from the file, rather than re-printing the first three lines as your working version does.