在Python中,关闭已打开的文件通常有以下几种方法:
1. 使用 `close()` 方法:
```python
file = open("example.txt", "r")
content = file.read()
print(content)
file.close() 关闭文件
2. 使用 `with` 语句:```pythonwith open("example.txt", "r") as file:
content = file.read()
print(content) 文件会在with语句结束时自动关闭
3. 使用 `try-finally` 块:
```python
file = open("example.txt", "r")
try:
content = file.read()
print(content)
finally:
file.close() 文件会在finally块结束时关闭
请确保在文件操作完成后及时关闭文件,以释放系统资源。

