Python 3 minor irritation
Alf P. Steinbach
alfps at start.no
Thu Feb 4 06:46:45 CET 2010
* Alf P. Steinbach:
> * Benjamin Kaplan:
>> On Wed, Feb 3, 2010 at 11:18 PM, Alf P. Steinbach <alfps at start.no> wrote:
>>> * Benjamin Kaplan:
>>>> The easiest way to solve this permanently, by the way, is to not use
>>>> relative paths. All it takes is one script to call os.chdir and the
>>>> script breaks. You can use __file__ and the os.path module to figure
>>>> out exactly where you are in an OS-agnostic way.
>>>> import os.path
>>>> #get the absolute path to the current script
>>>> abs_path = os.path.abspath(__file__)
>>> According to the docs: "On most platforms, this is equivalent to
>>> normpath(join(os.getcwd(), path))."
>> os.path.abspath will always work in this case (unless something
>> changes the current working directory before that statement runs)
> Which change is what you surmised as a cause of the original problem.
>> because __file__ is given either as an absolute path or as relative to
>> the current working directory.
> No, that's incorrect.
Oh sorry, now I see what you mean. I read it too literally. You mean that at
script startup __file__ is a valid relative or absolute path to the script.
But anyways, Windows Explorer doesn't change the current directory to that of
the associated program, at least not in Windows XP.
Where there *is* a difference with double-clicking the script is in the path
that ends up as __file__.
[snip my silly code counter-example + even more silly comment]
More information about the Python-list