在python中关闭文件方法有:1.使用close()函数关闭;2.使用with语句关闭;3.使用try-finally块关闭;
活动:慈云数据爆款香港服务器,CTG+CN2高速带宽、快速稳定、平均延迟10+ms 速度快,免备案,每月仅需19元!! 点击查看 在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()
vps云服务器
《怎么在python中关闭已打开的文件》来自互联网同行内容,若有侵权,请联系我们删除!
还没有评论,来说两句吧...