Error on close function Ex - 15

1 prompt = ("> ")

2 print(“Enter filename.”)
3 file3 = input(prompt)

4 print(f"Opening {file3}.")
5 print(open(file3).read())
6 file3.close()

I followed a study drill and added a close fuction at the end of the code, but python returned this error.

Traceback (most recent call last):
File “ex15_extra.py”, line 6, in
file3.close()

Can someone please explain this error. :confused:

Also Zed says that It’s is important to close files when we are done with them. What happens if I don’t close them?

Thanks and Regards
Shreshth Goyal

Hello and welcome to the forum @TheTechnocrat

I guess this is not all of your code for exercise 15.
So it is a little bit difficult to help you here because the error points to row 22 and you only provide us with six rows.

One thing I can see is on line1 there is missing a “input”

prompt = input(">")

I am sorry, the error points out to line 6. I just corrected it.

I just read EX-17 and in the 5th question in the Common Student Questions explains the question I asked.

Thanks for trying to help :smiley:

Thanks & Regards
Shreshth Goyal

You just have to make sure file3 is the type of thing you think it is. You’re doing this:

file3.close()

But wait, is file3 a file, or a string that holds the name of the file? Try this:

print(repr(file3))

What does it tell you?