English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
close() This method is used to close an opened file. After closing, the file can no longer be read or written. Otherwise, it will trigger ValueError Error. The close() method can be called multiple times.
When the file object is referenced to operate another file, Python will automatically close the previous file object. It is a good habit to close the file using the close() method.
The syntax of the close() method is as follows:
fileObject.close();
None
This method does not return any value.
The following example demonstrates the use of the close() method:
# Open the file fo = open("w3codebox.txt, "wb") print("The file name is: ", fo.name) # Close the file fo.close()
The output result of the above example is:
The file name is: w3codebox.txt