发布时间:2019-09-08 09:10:54编辑:auto阅读(1827)
#!/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获取当前目录路径和上级路径
48922
48032
38789
35918
30338
27118
26123
20956
20778
19137
648°
728°
699°
691°
666°
632°
729°
814°
920°
1126°