Re: __file__



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.

.



Relevant Pages

  • Relative path & image
    ... is possible use relative path in access? ... Is posible don´t put a path to the picture ... in the image control? ... The Database will be move frecuntly and it will give error in the form load ...
    (comp.databases.ms-access)
  • Re: __file__
    ... you get just the filename; ... contains a relative path, that's what you get; ... Whatever path worked to find and load the module, ... you get /zot/foo.py which is a nonsense. ...
    (comp.lang.python)
  • Re: CDHtmlDialog fail to load image in Vista
    ... such a comprehensive explanation. ... I chose to load all externally and ... rather than relative path as I would certainly make this mistake. ...
    (microsoft.public.vc.mfc)
  • Re: How to change directory/path in SBCL?
    ... | which have relative path to that file cannot be loaded ... the files you wish to load by using ... as the pathname to load. ... the underlying posix system (which may not even exist, ...
    (comp.lang.lisp)
  • Re: ASM Cassette Images in TRS-80 Emulator
    ... David Keil's TRS-80 Model III/4/4P emulator. ... One of the progress asterisks changes to a C, and the load prematurely stops. ... Picking apart the *.CAS file that I was trying to load had initially suggested that the filename was "SPWAR" There is other data in the file, of course, but that plaintext seemed like a good guess. ...
    (comp.sys.tandy)