Re: __file__
- From: "7stud" <bbxx789_05ss@xxxxxxxxx>
- Date: 11 Apr 2007 23:34:43 -0700
On Apr 11, 6:55 am, "John Machin" <sjmac...@xxxxxxxxxxx> wrote:
On Apr 11, 8:03 pm, "7stud" <bbxx789_0...@xxxxxxxxx> wrote:
Hi,
Thanks for the response.
On Apr 11, 12:49 am, "Gabriel Genellina" <gagsl-...@xxxxxxxxxxxx>
wrote:
__file__ corresponds to the filename used to locate and load the module,
whatever it is. When the module is found on the current directory
(corresponding to '' in sys.path), you get just the filename; if sys.path
contains a relative path, that's what you get; the same for any absolute
path.
Whatever path worked to find and load the module, that's stored as
__file__.
If you plan to use it, it's a good idea to make it early into an absolute
path (using os.path.abspath(__file__)) just in case the current directory
changes.
That last part doesn't seem to fit with your description above. What
does the current working directory have to do with the path that was
used to load a module? I would think the path that was used to load a
module is constant.
You are correct, but that is not what GG was talking about. Here is an
example of what he meant:
While your cwd is /bar, you load module foo from a *relative*( path,
e.g. ./foo.py. If you do the os.path.abspath("./foo.py") immediately
as recommended, you get the correct answer: /bar/foo.py. However if
you change your cwd to /zot, then do the os.path.abspath("./foo.py"),
you get /zot/foo.py which is a nonsense.
HTH,
John
Thanks for all the help.
.
- References:
- __file__
- From: 7stud
- Re: __file__
- From: Gabriel Genellina
- Re: __file__
- From: 7stud
- Re: __file__
- From: John Machin
- __file__
- Prev by Date: Re: [OT] Deferred jobs server as backend for web application
- Next by Date: Re: py script modify it's Modified Time?
- Previous by thread: Re: __file__
- Next by thread: How to get IP address of client from CGI module?
- Index(es):
Relevant Pages
|