FAQ
Hello,

Is ter any way to identify the file name and the path in which the
function is called.

for example: if the function definition is in the file c:\script1.py
and if I call this function in a script: c:\script2.py than in the
function definition part(script1.py) some how I need to know that its
called in c:\script2.py.

Thanks,
Giri

Search Discussions

  • Almar Klein at Nov 28, 2008 at 8:22 am
    You could pass it as an argument:

    ==== c:\script1.py
    foo(whatever_arguments_you_need, caller=None):
    ....

    ==== c:\script2.py
    import script1
    script1.foo(blabla, caller=__file__)

    Other than that I would not know. Maybe the inspect module can
    help you but as far as I can see, it doesn't. I'm also not sure if it
    would be a nice way to solve your problem (what is your problem?),
    if it is even possible. Additionally, the function could be called from
    the command line rather than from a file.

    Almar


    2008/11/28 Girish <girish.cfc at gmail.com>
    Hello,

    Is ter any way to identify the file name and the path in which the
    function is called.

    for example: if the function definition is in the file c:\script1.py
    and if I call this function in a script: c:\script2.py than in the
    function definition part(script1.py) some how I need to know that its
    called in c:\script2.py.

    Thanks,
    Giri
    --
    http://mail.python.org/mailman/listinfo/python-list
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20081128/07c10e5c/attachment.htm>
  • Peter Otten at Nov 28, 2008 at 8:46 am

    Girish wrote:

    Is ter any way to identify the file name and the path in which the
    function is called.

    for example: if the function definition is in the file c:\script1.py
    and if I call this function in a script: c:\script2.py than in the
    function definition part(script1.py) some how I need to know that its
    called in c:\script2.py.
    You can inspect the stack:

    import sys
    import inspect

    def some_func():
    print inspect.getfile(sys._getframe(1))

    Peter
  • Girish at Nov 28, 2008 at 9:48 am

    On Nov 28, 1:46?pm, Peter Otten wrote:
    Girish wrote:
    Is ter any way to identify the file name and the path in which the
    function is called.
    for example: if the function definition is in the file c:\script1.py
    and if I call this function in a script: c:\script2.py than in the
    function definition part(script1.py) some how I need to know that its
    called in c:\script2.py.
    You can inspect the stack:

    import sys
    import inspect

    def some_func():
    ? ? print inspect.getfile(sys._getframe(1))

    Peter
    Thanks a lot Peter.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedNov 28, '08 at 5:43a
activeNov 28, '08 at 9:48a
posts4
users3
websitepython.org

People

Translate

site design / logo © 2021 Grokbase