怎么在python中关闭已打开的文件
在python中关闭文件方法有:1.使用close()函数关闭;2.使用with语句关闭;3.使用try-finally块关闭;
在python中关闭文件方法有以下几种
1.使用close()函数关闭
# 打开文件
fo = open("runoob.txt", "wb")
print "文件名为: ", fo.name
# 关闭文件
fo.close()
2.使用with语句关闭
with open('dog_breeds.txt') as reader:
3.使用try-finally块关闭
reader = open('dog_breeds.txt')
try:
finally:
reader.close()
版权声明
本文仅代表作者观点,不代表米安网络立场。
上一篇:php计划任务定时执行的方法有哪些 下一篇:phpnow卸载干净的步骤是什么
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。