How to close a file using the close () method in Python Programming
This article describes how to close a file using the close () method in Python programming. It is a basic knowledge in Python programming. For more information, see
Close () method to close open files. The closed file cannot read or write more things. If the file is closed, any operation will cause a ValueError. However, you can call close () multiple times.
Python is automatically disabled when the referenced object of a file is reassigned to another file. It uses the close () method to close a file.
Syntax
The syntax of the close () method is as follows:
?
Parameters
NA
Return Value
This method does not return any value
Example
The following example shows how to use the close () method.
?
1 2 3 4 5 6 7 8 |
#! /Usr/bin/python # Open a file Fo = open ("foo.txt", "wb ") Print "Name of the file:", fo. name # Close opend file Fo. close () |
When we run the above program, it will produce the following results:
?
1 |
Name of the file: foo.txt |