好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python编程中用close()方法关闭文件的教程

close()方法方法关闭打开的文件。关闭的文件无法读取或写入更多东西。文件已被关闭之后任何操作会引发ValueError。但是调用close()多次是可以的。

Python自动关闭,当一个文件的引用对象被重新分配给另外一个文件。它使用close()方法来关闭一个文件一个很好的做法。
语法

以下是close()方法的语法:

fileObject.close();

 

参数

NA

返回值

此方法不返回任何值
例子

下面的例子显示了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

 

查看更多关于Python编程中用close()方法关闭文件的教程的详细内容...

  阅读:42次