Python File close() Method
The method close() closes the opened file. A closed file cannot be read or written any more. Any operation, which requires that the file be opened will raise a ValueError after the file has been closed. Calling close() more than once is allowed.
Python automatically closes a file when the reference object of a file is reassigned to another file. It is a good practice to use the close() method to close a file.
Following is the syntax for close() method:
This method does not return any value.
The following example shows the usage of close() method.
#!/usr/bin/python # Open a file fo = open("foo.txt", "wb") print "Name of the file: ", fo.name # Close opend file fo.close()
Let us compile and run the above program, this will produce the following result:
Name of the file: foo.txt