Is a file closed?

EasyFile Handling Programs · 23 of 38

Write a Python program to check whether a file is closed.

Advertisement
Output
False
True
inside with: False
after with: True

Approach

  1. The closed attribute.

Solution

Python
f = open("temp.txt", "w")print(f.closed)f.close()print(f.closed) with open("temp.txt") as g:    print("inside with:", g.closed)print("after with:", g.closed)
Output
False
True
inside with: False
after with: True

Learn the concept

New to this topic? Read the lesson, then come back.

More file handling programs

← All 38 File Handling Programs

Leave a Comment