Python file方法close()关闭打开的文件。关闭的文件不能再读取或写入。要求打开文件的任何操作都将在文件关闭后引发ValueError。允许多次调用close()。
将文件的引用对象重新指定给另一个文件时,Python会自动关闭文件。使用close()方法关闭文件是一种很好的做法。
file.close() - 语法
fileObject.close();
file.close() - 示例
以下示例显示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