Home » Python

Python File closed Property with Example

Python File closed Property: Here, we are going to learn about the closed Property, how to check whether a file is closed or not in Python?
Submitted by IncludeHelp, on December 24, 2019

File closed Property

closed Property is an inbuilt property of File object (IO object) in Python, it can be used to check whether a file object (i.e. a file) is closed or not, this is a read-only property and returns a Boolean value (True – if file is closed, False – if file is not closed).

Syntax:

    file_object.closed

Parameter(s):

  • None

Return value:

The return type of this method is <class 'bool'>, it returns a Boolean value.

Example:

# Python File closed Property with Example

# opening files in various modes
file1 = open("myfile1.txt", "w")
file2 = open("myfile3.txt", "a")
file3 = open("myfile4.txt", "wb")

# checking files are closed or not
print("Before closing the files...")
print("file1.closed: ", file1.closed)
print("file2.closed: ", file2.closed)
print("file3.closed: ", file3.closed)

# closing the files
file1.close()
file2.close()
file3.close()

# checking files are closed or not
print("After closing the files...")
print("file1.closed: ", file1.closed)
print("file2.closed: ", file2.closed)
print("file3.closed: ", file3.closed)

Output

Before closing the files...
file1.closed:  False
file2.closed:  False
file3.closed:  False
After closing the files...
file1.closed:  True
file2.closed:  True
file3.closed:  True



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.