Python File close() 方法

Python 教程 · 2019-02-03 13:44:35

概述

close() 方法用于关闭一个已打开的文件。关闭后的文件不能再进行读写操作, 否则会触发 ValueError 错误。 close() 方法允许调用多次。

当 file 对象,被引用到操作另外一个文件时,Python 会自动关闭之前的 file 对象。 使用 close() 方法关闭文件是一个好的习惯。

语法

close() 方法语法如下:

fileObject.close();

参数

返回值

该方法没有返回值。

实例

以下实例演示了 close() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 打开文件
fo = open("codercto.txt", "wb")
print "文件名为: ", fo.name

# 关闭文件
fo.close()

以上实例输出结果为:

文件名为:  codercto.txt

点击查看所有 Python 教程 文章: https://www.codercto.com/courses/l/8.html

查看所有标签

Python Data Structures and Algorithms

Python Data Structures and Algorithms

Benjamin Baka / Packt Publishing / 2017-5-30 / USD 44.99

Key Features A step by step guide, which will provide you with a thorough discussion on the analysis and design of fundamental Python data structures.Get a better understanding of advanced Python c......一起来看看 《Python Data Structures and Algorithms》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码