Re: Retrieve source filename

Discussion in 'Python' started by Irmen de Jong, Jul 31, 2003.

  1. Shrom wrote:

    > I know that you can retrieve main file name with sys.argv[0] and file name
    > for a module with modulename.__file__ but is there a common way to retrieve
    > file name for both main program and modules as the __FILE__ constant in PHP
    > ?


    I don't know if it's the "right" way, but this works:

    import inspect
    print inspect.getfile(inspect.currentframe())


    --Irmen
    Irmen de Jong, Jul 31, 2003
    #1
    1. Advertising

  2. Irmen de Jong <> writes:

    > Shrom wrote:
    >
    >> I know that you can retrieve main file name with sys.argv[0] and file name
    >> for a module with modulename.__file__ but is there a common way to retrieve
    >> file name for both main program and modules as the __FILE__ constant in PHP
    >> ?

    >
    > I don't know if it's the "right" way, but this works:
    >
    > import inspect
    > print inspect.getfile(inspect.currentframe())
    >

    This one should also work:

    if __name__ == "__main__":
    import sys
    __file__ = sys.argv[0]

    Thomas
    Thomas Heller, Jul 31, 2003
    #2
    1. Advertising

  3. Irmen de Jong

    Greg Fortune Guest

    If you are working with an executable script (ie, __name__ == "__main__"),

    import sys
    import os
    os.path.abspath(sys.argv[0])

    will give it to you.

    Otherwise,

    import imp
    imp.find_module(__name__)[1]

    will work.

    It's pretty simple to detect between the two cases by simply checking
    __name__.

    Greg Fortune
    Fortune Solutions

    Irmen de Jong wrote:

    > Shrom wrote:
    >
    >> I know that you can retrieve main file name with sys.argv[0] and file
    >> name for a module with modulename.__file__ but is there a common way to
    >> retrieve file name for both main program and modules as the __FILE__
    >> constant in PHP ?

    >
    > I don't know if it's the "right" way, but this works:
    >
    > import inspect
    > print inspect.getfile(inspect.currentframe())
    >
    >
    > --Irmen
    Greg Fortune, Jul 31, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    Extract filename from a filename typed by user

    =?Utf-8?B?Sm9l?=, Aug 23, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,002
    Travis Murray
    Aug 24, 2004
  2. Replies:
    1
    Views:
    1,430
    Roland de Ruiter
    Jun 15, 2006
  3. Steven Taschuk

    Re: Retrieve source filename

    Steven Taschuk, Aug 1, 2003, in forum: Python
    Replies:
    3
    Views:
    301
    Steven Taschuk
    Aug 1, 2003
  4. Ed
    Replies:
    10
    Views:
    45,726
    alok000707
    Jul 13, 2010
  5. Diarmaid
    Replies:
    3
    Views:
    166
    Evertjan.
    May 12, 2004
Loading...

Share This Page