Python Practice › File Handling Programs › Program 23
Is a file closed?
Write a Python program to check whether a file is closed.
Output
False True inside with: False after with: True
Approach
- The
closedattribute.
Solution
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.