69pao国产精品视频-久久精品一区二区二三区-精品国产精品亚洲一本大道-99国产综合一区久久

Python File close() 方法

python file close() 方法

python file 方法python file 方法

close() 方法用于關(guān)閉一個(gè)已打開的文件。關(guān)閉后的文件不能再進(jìn)行讀寫操作, 否則會(huì)觸發(fā) valueerror 錯(cuò)誤。 close() 方法允許調(diào)用多次。

當(dāng) file 對象,被引用到操作另外一個(gè)文件時(shí),python 會(huì)自動(dòng)關(guān)閉之前的 file 對象。 使用 close() 方法關(guān)閉文件是一個(gè)好的習(xí)慣。

語法

close() 方法語法如下:

fileobject.close();

參數(shù)

返回值

該方法沒有返回值。

實(shí)例

以下實(shí)例演示了 close() 方法的使用:

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

# 打開文件
fo = open("codebaoku.txt", "wb")
print "文件名為: ", fo.name

# 關(guān)閉文件
fo.close()

以上實(shí)例輸出結(jié)果為:

文件名為:  codebaoku.txt

python file 方法python file 方法

下一節(jié):python file flush() 方法

python 教程

相關(guān)文章