• TraceBack


    #!/usr/bin/env python3
    # _*_ coding:utf-8 _*_
    # ========================================================
    # Module         :  ExceotionTraceBack
    # Author         :  luting
    # Create Date    :  2018/6/7
    # Amended by     :  luting
    # Amend History  :  2018/6/7
    # =======================================================
    import traceback
    
    
    # 捕捉异常基本操作
    try:
        with open('xxx.txt', 'r') as file:
            for line in file:
                print(line)
    except Exception as error:
        print(error)
    
    
    # 使用traceback -> 能清楚具体哪一行代码出错
    try:
        with open('xxx.txt', 'r') as file:
            for line in file:
                print(line)
    except Exception:
        # print_exc 直接打印
        traceback.print_exc(file=open('error.txt', 'w+'))
    
    try:
        with open('xxx.txt', 'r') as file:
            for line in file:
                print(line)
    except Exception:
        # format_exc 返回字符串
        print(traceback.format_exc())
  • 相关阅读:
    文件管理后章
    文件管理前章
    文本处理三剑客
    匿名函数以及应用、三元表达式
    认识docker以及常用操作
    字符编码
    赋值与深浅拷贝
    Python基本运算符
    LVM配置
    Linux磁盘管理
  • 原文地址:https://www.cnblogs.com/xiaoxiaolulu/p/9149450.html
Copyright © 2020-2023  润新知