open
- 打开文件
file=open(“文件名”,“读写模式”)
- 操作文件
代码段
- 关闭文件
file.close()
注意事项:使用open方法,文件操作完毕之后必须手动关闭,否则长期保持对文件的连接状态,造成内存溢出的现象发生。
with open
- 打开文件
with open (“文件名”,“读写模式”) as file:
- 操作文件
代码段
- 关闭文件(自动关闭)
open和with open的用法,你弄明白了吗?
阅读 62
2022-02-18
file=open(“文件名”,“读写模式”)
代码段
file.close()
with open (“文件名”,“读写模式”) as file:
代码段
相关推荐
精彩评论(0)