How to find number of line that is currently executing?

koranthala koranthala at gmail.com
Sat Oct 10 14:57:08 CEST 2009


On Oct 10, 8:54 am, Chris Rebert <c... at rebertia.com> wrote:
> On Fri, Oct 9, 2009 at 8:46 PM, Dr. Phillip M. Feldman
>
> <pfeld... at verizon.net> wrote:
>
> > I would like to put a statement on line N of my program that prints the line
> > number that is currently executing. This may sound fairly trivial, but I
> > don't want to hard code the line number because N will change if lines are
> > inserted or deleted above that point. Any advice will be appreciated.
>
> If you're doing this for debugging, it's often more convenient to just
> come up with a unique string you can grep for, but anyway:
>
> import traceback
> print traceback.extract_stack()[-1][1]
>
> Cheers,
> Chris
> --http://blog.rebertia.com

You can also use the logging module for good effect.
say: logging.info('Random Text')
A log generated this way also contains the line number.



More information about the Python-list mailing list