filename of calling function?

Joel Davis callmeclaudius at gmail.com
Sat Nov 28 18:04:40 CET 2009


On Nov 28, 11:40 am, Phlip <phlip2... at gmail.com> wrote:
> On Nov 28, 8:19 am, Phlip <phlip2... at gmail.com> wrote:
>
>
>
> > Consider these two python modules:
>
> > aa.py
>
> > def a():
> >     print '?'
>
> > bb.py
> >   import aa
>
> > def bb():
> >   aa.a()
>
> > bb()
>
> > How do I make the print line emit the filename of bb.py? (It could be
> > anything.)
>
>         try:
>             raise None
>         except:
>             import sys
>             from traceback import extract_tb, extract_stack
>             frame = sys.exc_info()[2].tb_frame.f_back
>             calling_file = extract_stack(frame, 2)[1][0]

code works perfectly except on my system both the indexes need to be 0
(eg: "extract_stack(frame, 2)[0][0]")



More information about the Python-list mailing list