Python File close() Method

Example

Close the file after opening it:

f = open("demofile.txt", "r")
print(f.read())
f.close()

Running an instance

Definition and Usage

The close() method closes the opened file.

You should always close the file, and in some cases, changes made to the file may not be displayed until the file is closed due to buffering.

Syntax

file.close()

Parameter value

No parameters.