Help with case cloesd


#1


https://www.codecademy.com/courses/python-intermediate-en-OGNHh/1/5?curriculum_id=4f89dab3d788890003000096

Trying to test if file is closed and to close it if not but keep getting
"Traceback (most recent call last):
File "python", line 5, in
TypeError: 'bool' object is not callable"
and "Oops, try again. You don't need any parentheses after 'closed'—just my_file.closed should do the trick."
But when I remove the paentheses I get
"Oops, try again. Did you call my_file.close() in the event that my_file.closed returns False?"


Any ideas?


with open("text.txt", "w") as my_file:
    my_file.write("Helli")
     
    if not my_file.closed:
        my_file.closed()
    print my_file.closed


#2

print my_file.closed

This line should be,
if my_file is not my_file.closed:
and not,
if not my_file.closed:

And here,

You close the file with the syntax, my_file.close not my_file.closed.

And that print statement at the end should only include
my_file


#3

Thank you turned out I just needed to change closed() to close()


#4

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