The Python file close() method is used to close the specified file. Please note that, file must has opened before using the method.
No parameter is required.
In the below example, Python file close() method is used to close an opened file called MyFile. The file read() method before closing the file will return the content of the file and after closing the file will raise an exception.
MyFile = open("python_test.txt", "r") #read content before closing the file print(MyFile.read()) MyFile.close() #read content after closing the file print(MyFile.read())
Python is a programming language. Learning Python is fun. ValueError: I/O operation on closed file.