当前位置:主页 > 软件编程 > Python代码 >

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

时间:2021-03-30 09:06:16 | 栏目:Python代码 | 点击:

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

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

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

fileObject.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

 

您可能感兴趣的文章:

相关文章