Python File close() method
Overview
close() The method closes an open file. A closed file can no longer be read or written, otherwise a ValueError error is raised. The close() method may be called multiple times.
When a file object is referenced to operate on another file, Python automatically closes the previous file object. Closing files with close() is a good habit.
Syntax
The close() method syntax is as follows:
fileObject.close();
Parameters
-
None
Return Value
This method has no return value.
Example
The following example shows how to use the close() method:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# Open file
fo = open("runoob.txt", "wb")
print "文件名为: ", fo.name
# Close file
fo.close()
The output of the above example is:
文件名为: runoob.txt
Python File methods