close()method是Python中的内置方法,用于刷新和关闭IO对象(文件)。如果我们关闭已关闭的文件,则没有任何效果。
语法:
file_object.close()
Parameter(s):
它不接受任何参数。
返回值:
此方法的返回类型为<class'NoneType'>,它不返回任何内容。
范例1:
# 带有示例的Python File close()方法 # 以写模式打开文件 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:
# 带有示例的Python File close()方法 # 以写模式打开文件 f = open("Hello.txt", "w") print("file opened...") # 关闭档案 f.close() # 关闭已关闭的文件 f.close() print("file closed...")
输出结果
file opened... file closed...