Python3 file.close()方法

close()方法关闭打开的文件。一个关闭的文件无法读取或写入数据内容。该文件已被关闭后的任何操作,文件打开会引发ValueError。 close()可以允许多次调用。

Python3自动关闭文件的参考对象分配给另一个文件。 使用 close()方法关闭文件是一个很好的做法。

语法

以下是close()方法的语法 -
fileObject.close()

参数

  • NA

返回值

此方法不返回任何值。

示例

下面的示例说明 close()方法的使用。
#!/usr/bin/python3

# Open a file
fo = open("foo.txt", "wb")
print ("Name of the file: ", fo.name)

# Close opened file
fo.close()
当我们运行上面的程序,会产生以下结果 -
Name of the file:  foo.txt

上一篇: Python3模块 下一篇: Python3文件方法