day6_异常捕捉
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/7/11 14:14
# @Author : 大坏男孩
# @File : day6_异常捕捉.py
# @Software: PyCharm
# @license : Copyright(C), 未来集团有限公司
# @Contact : 110@163.com
"""
异常捕捉:
try:try监听的代码如果发生了异常,则执行except下的代码
如果未发生异常,则不执行except下的代码
"""
# try:
# print(加油)
# except Exception as e:
# print("加油没有定义")
import day6_logging模块 as day6
l = day6.logs()
def read_file(filename):
try:
f = open(filename,"r+",encoding="gbk")
data = f.read()
print("文件已成功读取",data)
return data,f
except FileNotFoundError as e:
open(filename,"w+",encoding="utf8").close()
data,f = read_file(filename)
return data,f
except Exception as e:
l.error(e)
return False
datas = read_file("周杰伦.txt")
if datas != False:
if datas[0]:
print(datas[0])
else:
datas[1].write("陪了我整个青春!!!")
else:
print("read_file函数发生未知错误!")
一行代码一行诗

浙公网安备 33010602011771号