getting source code line of error?

Marco Sulla Marco.Sulla.Python at gmail.com
Fri Nov 19 14:13:52 EST 2021


Have you tried the logger module and the format options?

On Fri, 19 Nov 2021 at 19:09, Ulli Horlacher
<framstag at rus.uni-stuttgart.de> wrote:
>
> I am trying to get the source code line of the last error.
> I know traceback.format_exc() but this contains much more information, e.g.:
>
> Traceback (most recent call last):
>   File "./error.py", line 18, in main
>     x=1/0
> ZeroDivisionError: division by zero
>
> I could extract the source code line with re.search(), but is there an
> easier way?
>
>
> I have:
>
>       exc_type,exc_str,exc_tb = sys.exc_info()
>       fname = exc_tb.tb_frame.f_code.co_filename
>       line = exc_tb.tb_lineno
>       print('%s in %s line %d' % (exc_str,fname,line))
>
> But I also want to output the line itself, not only its number.
>
> --
> Ullrich Horlacher              Server und Virtualisierung
> Rechenzentrum TIK
> Universitaet Stuttgart         E-Mail: horlacher at tik.uni-stuttgart.de
> Allmandring 30a                Tel:    ++49-711-68565868
> 70569 Stuttgart (Germany)      WWW:    http://www.tik.uni-stuttgart.de/
> --
> https://mail.python.org/mailman/listinfo/python-list


More information about the Python-list mailing list