#!/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语句,关闭文件