file.close()
描述 (Description)
close()方法关闭打开的文件。 无法再读取或写入已关闭的文件。 任何需要打开文件的操作都会在文件关闭后引发ValueError 。 允许多次调用close()。
当文件的引用对象被重新分配给另一个文件时,Python会自动关闭文件。 使用close()方法关闭文件是一种很好的做法。
语法 (Syntax)
以下是close()方法的语法 -
fileObject.close();
参数 (Parameters)
NA
返回值 (Return Value)
此方法不返回任何值。
例子 (Example)
以下示例显示了close()方法的用法。
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name
# Close opend file
fo.close()
当我们运行上面的程序时,它产生以下结果 -
Name of the file: foo.txt