在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()


发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。