发布时间:2019-09-08 09:10:54编辑:auto阅读(1906)
#!/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语句,关闭文件
上一篇: Python 中的 socket 模块
下一篇: python获取当前目录路径和上级路径
50151
49419
40069
37102
31507
28344
27294
22083
22046
20384
610°
446°
436°
624°
575°
1177°
2218°
2043°
1596°
3238°