Python File close() Method

Instance

Close the file after opening it:

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

Run 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.