__file__

7stud bbxx789_05ss at yahoo.com
Thu Apr 12 08:34:43 CEST 2007


On Apr 11, 6:55 am, "John Machin" <sjmac... at lexicon.net> wrote:
> On Apr 11, 8:03 pm, "7stud" <bbxx789_0... at yahoo.com> wrote:
>
>
>
> > Hi,
>
> > Thanks for the response.
>
> > On Apr 11, 12:49 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
> > 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.




More information about the Python-list mailing list