Latest web development tutorials

Python File close () method

Python File (File) method Python File (File) method


Outline

close () method is used to close an open file.After the file is closed can not read and write, otherwise it will triggerValueErrorerrors. close () method allows multiple calls.

When the file object is referenced to operate another file, Python will automatically close the previous file objects. Using the close () method to close the file is a good habit.

grammar

close () method has the following syntax:

fileObject.close();

parameter

  • no

return value

This method has no return value.

Examples

The following example demonstrates the use of close () method:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 打开文件
fo = open("w3big.txt", "wb")
print "文件名为: ", fo.name

# 关闭文件
fo.close()

The above example output is:

文件名为:  w3big.txt

Python File (File) method Python File (File) method