带有示例的Python File close()方法
档案close()方式
close()method是Python中的内置方法,用于刷新和关闭IO对象(文件)。如果我们关闭已关闭的文件,则没有任何效果。
语法:
file_object.close()
Parameter(s):
它不接受任何参数。
返回值:
此方法的返回类型为<class'NoneType'>,它不返回任何内容。
范例1:
#带有示例的PythonFileclose()方法
#以写模式打开文件
f = open("Hello.txt", "w")
print("file opened...")
print(f) #打印文件详细信息
#关闭档案
f.close()
print("file closed...")输出结果
file opened... <_io.TextIOWrapper name='Hello.txt' mode='w' encoding='UTF-8'> file closed...
范例2:
#带有示例的PythonFileclose()方法
#以写模式打开文件
f = open("Hello.txt", "w")
print("file opened...")
#关闭档案
f.close()
#关闭已关闭的文件
f.close()
print("file closed...")输出结果
file opened... file closed...
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短