SORU
14 AĞUSTOS 2009, Cuma


Python bir istisna yakaladığımda, ne yazın, dosya ve satır numarasını alabilir miyim?

Böyle bir baskı olacağını istisna yakalama:

Traceback (most recent call last):
  File "c:/tmp.py", line 1, in <module>
    4 / 0
ZeroDivisionError: integer division or modulo by zero

İçine biçimlendirmek istiyorum:

ZeroDivisonError, tmp.py, 1

CEVAP
14 AĞUSTOS 2009, Cuma


import sys, os

try:
    raise NotImplementedError("No error")
except Exception as e:
    exc_type, exc_obj, exc_tb = sys.exc_info()
    fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
    print(exc_type, fname, exc_tb.tb_lineno)

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • campos9896

    campos9896

    24 Mart 2012
  • DavidParody

    DavidParody

    17 EKİM 2009
  • Mismag822 - The Card Trick Teacher

    Mismag822 -

    18 EKİM 2008