* Benjamin Kaplan:
Read my first paragraph again- it has absolutely nothing to do with
Python. It has to do with how the Windows is running Python 3 on the
OP's computer.
I'm not sure what you're arguing here.
But anyway, first, I haven't stated that "it" is (only) an issue with Python: on
the contrary, I explained how the program is run by Windows Explorer, where in
Windows XP the full path is passed by Windows Explorer, and how that results in
(can result in) a full path in __file__ when a script is run by double-clicking.
Second, as stated your hypothesis about current directory is Wrong(TM).
So the explanation of an incorrect result involves not only Windows Explorer,
the Windows version, and Python, but also the OP's code is involved.
Hence the request for a minimal, complete example -- which is nearly always a
good idea.
The OP's description said that it was looking for the
file .\\myfile.xml in C:\Python31 which means it translated '.', the
current working directory, to be C:\Python31 and not the directory the
script is in.
I fail to reproduce that behavior.
By the way, are you the OP (just with another name)?
If not, have you reproduced the described behavior? Then kindly post the code.
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.
Hello.
because __file__ is given either as an absolute path or as relative to
the current working directory.
No, that's incorrect.
<code py="3.1">
import os
print( "Current directory: [" + os.getcwd() + "]" )
print( "__file__ = " + __file__ )
os.chdir( "blah" )
print( "Current directory: [" + os.getcwd() + "]" )
print( "__file__ = " + __file__ )
</code>
<example>
C:\Documents and Settings\Alf\test> python curdir.py
Current directory: [C:\Documents and Settings\Alf\test]
__file__ = curdir.py
Current directory: [C:\Documents and Settings\Alf\test\blah]
__file__ = curdir.py <-- *Not* relative to cwd.
C:\Documents and Settings\Alf\test> _
</example>
[snip]
Except that if the cwd is not the directory the script is running in,
__file__ should still point to it either through an absolute path
(highly likely since it's run through Windows Explorer) or some
(however convoluted) relative path.
Don't know about "should", but if you're talking reality, no, that's incorrect;
see above.
[snip]
Cheers & hth.,
- Alf