Easy Tutorial
❮ Ref Math Isqrt Ref Set Clear ❯

Python3 File close() Method

Python3 File Methods


Overview

The close() method is used to close an already opened file. Once closed, the file cannot be read from or written to anymore, otherwise a ValueError will be triggered. The close() method can be called multiple times.

When a file object is referenced to operate on another file, Python automatically closes the previous file object. It is a good practice to close a file using the close() method.

Syntax

The syntax for the close() method is as follows:

fileObject.close();

Parameters

-

None

Return Value

This method does not return any value.

Example

The following example demonstrates the use of the close() method:

#!/usr/bin/python3

# Open file
fo = open("tutorialpro.txt", "wb")
print("File name: ", fo.name)

# Close file
fo.close()

The output of the above example is:

File name:  tutorialpro.txt

Python3 File Methods

❮ Ref Math Isqrt Ref Set Clear ❯