Re: Retrieve source filename

Discussion in 'Python' started by Steven Taschuk, Aug 1, 2003.

  1. Quoth Shrom:
    > 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
    > ?


    As of 2.3, the __main__ module also has a __file__ attribute, so
    you can just do
    sys.modules[__name__].__file__
    The (an?) exception is when __main__ is the text in a 'python -c'
    command -- then there is, of course, no file name. And for
    compatibility with versions < 2.3, you have to check for the case
    __name__ == '__main__' and go to sys.argv[0].

    --
    Steven Taschuk 7\ 7'Z {&~ .
    Y r --/hG-
    (__/ )_ 1^1`
     
    Steven Taschuk, Aug 1, 2003
    #1
    1. Advertising

  2. Steven Taschuk

    Shrom Guest

    "Steven Taschuk" <> a écrit dans le message de
    news:...
    > Quoth Shrom:

    [SNIP]
    > As of 2.3, the __main__ module also has a __file__ attribute, so
    > you can just do
    > sys.modules[__name__].__file__
    > The (an?) exception is when __main__ is the text in a 'python -c'
    > command -- then there is, of course, no file name. And for
    > compatibility with versions < 2.3, you have to check for the case
    > __name__ == '__main__' and go to sys.argv[0].


    Thanks very much that's what i was searching.
     
    Shrom, Aug 1, 2003
    #2
    1. Advertising

  3. Bernhard Herzog, Aug 1, 2003
    #3
  4. Quoth Bernhard Herzog:
    > Steven Taschuk <> writes:

    [...]
    > > sys.modules[__name__].__file__

    >
    > Why so complicated? Just as __name__, __file__ is simply a global
    > variable in the module, so you can use it directly.


    Heh. Good catch.

    --
    Steven Taschuk
    "What I find most baffling about that song is that it was not a hit."
    -- Tony Dylan Davis (CKUA)
     
    Steven Taschuk, Aug 1, 2003
    #4
    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,043
    Travis Murray
    Aug 24, 2004
  2. Replies:
    1
    Views:
    1,536
    Roland de Ruiter
    Jun 15, 2006
  3. Irmen de Jong

    Re: Retrieve source filename

    Irmen de Jong, Jul 31, 2003, in forum: Python
    Replies:
    2
    Views:
    321
    Greg Fortune
    Jul 31, 2003
  4. Ed
    Replies:
    10
    Views:
    46,267
    alok000707
    Jul 13, 2010
  5. Diarmaid
    Replies:
    3
    Views:
    196
    Evertjan.
    May 12, 2004
Loading...

Share This Page