Python file close() method
Python File close() Method
The close() method is used to close an open file. A closed file cannot be read from or written to. If the file is closed, any operations that require the file to be open will result in a ValueError error. The close() method can be called multiple times.
Python automatically closes a file when the file reference object is reassigned to another file. It is good practice to use the close() method to close a file.
Syntax
The syntax of the close() method is as follows:
fileObject.close()
Parameters
NA
Return Value
This method does not return any value.
Example
The following example demonstrates the use of the close() method.
# Open a file
fo = open("foo.txt", "wb")
print ("Name of the file: ", fo.name)
# Close the opened file
fo.close()
When running the above program, the following output is produced –
Name of the file: foo.txt