Python 官方文档:入门教程 => 点击学习
#!/usr/bin/python 'if error occur when open a file or readline' try: try: ccfile = open('file9',
#!/usr/bin/python
'if error occur when open a file or readline'
try:
try:
ccfile = open('file9','r')
content = ccfile.readlines()
except ioError:
log.write('no content \n')
finally:
ccfile.close()
注释:无论try语句中是否有异常,finally语句都会执行!
然而try-else语句是,只有在try语句没有异常,才会执行else语句!
我们尝试打开一个文件,不管是在打开时出现异常还是在readlines时出现异常,我们都要执行finally语句,关闭文件
--结束END--
本文标题: python中try-finally语句
本文链接: https://www.lsjlt.com/news/188728.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0