在 Python 中,可以使用 built-in 的 open()
函数来打开文件,并使用 .close()
方法来关闭文件。如果要打开文件并将其设为只读模式,可以使用 open(file_name, 'r')
。而要将文件关闭只读,可以使用 file_object.close()
方法。
示例:
# 打开文件
file_object = open('example.txt', 'r')
# 在文件上执行操作
...
# 关闭文件
file_object.close()
在 Python 中,可以使用 built-in 的 open()
函数来打开文件,并使用 .close()
方法来关闭文件。如果要打开文件并将其设为只读模式,可以使用 open(file_name, 'r')
。而要将文件关闭只读,可以使用 file_object.close()
方法。
示例:
# 打开文件
file_object = open('example.txt', 'r')
# 在文件上执行操作
...
# 关闭文件
file_object.close()