Getting the home directory in Python and a bug in os.path.expanduser
Edward Diener
diener896092_no_spam_here at bellsouth.net
Sat Jul 7 15:25:50 EDT 2007
Josiah Carlson wrote:
> Edward Diener wrote:
>> What is the generic operating system way of getting the home directory ?
>>
>> I am guessing it is os.path.expanduser("~"). Is there a better way or
>> an alternate way ?
>>
>> If it is as I surmise, the aforementioned expanduser("~") of os.path
>> seems incorrect to me under Windows. The document says:
>>
>> "On Windows, only "~" is supported; it is replaced by the environment
>> variable HOME or by a combination of HOMEDRIVE and HOMEPATH."
>>
>> But HOME is never the home directory for Windows, only the combination
>> of HOMEDRIVE and HOMEPATH is valid, which is always set. If MSYS is
>> installed under Windows, where HOME must be set to the MSYS home
>> directory for a given user in order to emulate Linux/Unix, attempting
>> to use os.path.expanduser("~") will incorrectly return the MSYS home
>> directory for a given user rather than the Windows home directory for
>> the logged in user. So I think the os.path.expanduser("~") works
>> incorrectly in this case and needs to be fixed, else you are telling
>> users never to use MSYS under Windows.
>
> Some people have "sane" values for HOME on Windows.
And some people use Linux/Unix emulation software on Windows where HOME
has nothing to do with the Windows home directory, but everything to do
with the emulation's notion of a "home" directory. In fact Microsoft may
well have anticipated this by automatically generating HOMEDRIVE and
HOMEPATH based on the user's home directory when he logs on.
> That's the only
> reason why it was included in expanduser(). The current trunk version
> of ntpath offers HOME, USERPROFILE or HOMEDRIVE+HOMEPATH, as well as the
> expansion of ~/extra/stuff .
There is no problem checking the other values but on Windows
HOMEDRIVE+HOMEPATH should always be first, USERPROFILE should be second
if either HOMEDRIVE or HOMEPATH does not exist ( which is extremely
unlikely), and finally HOME should only be used if the others fail. I
will even go for USERPROFILE coming before the check for HOMEDRIVE and
HOMEPATH, although I think it is wrong because the notion of a "home"
directory on Windows may not stay tied to a user profile ( in fact they
may be already different on Vista, which I do not have, for all I know
). But at least USERPROFILE is generated by Windows, like HOMEDRIVE and
HOMEPARTH.
Using HOME as the first option on Windows is definitely wrong,
especially for the reason I pointed out, that Linux/Unix emulation
systems on Windows have the user set HOME to the emulation's "home"
directory, and that is definitely not the Windows "home" directory. HOME
is of course perfectly viable in the Linux/Unix world.
Probably most reliable on Windows is a Windows API function, if it
exists, for getting the home directory, as opposed to using environment
variables, but I can not find any Windows API for it at present.
>
> If you would like to get rid of Python's support of HOME, please post a
> bug report or feature request on the sourceforge tracker.
I realized I could post a bug report after I posted my OP, so I
subsequently posted a bug report on the sourceforge tracker. I do not
need a new feature, but only to have the current feature, which is
expanduser, work correctly on Windows. It is currently a serious problem
for Window's users, who have Linux/Unix emulation software on their
syetem, running a Python script which correctly uses expanduser to get
the Windows home directory and ends up with the wrong location.
Thank you for responding. I hope Python will fix this problem.
More information about the Python-list
mailing list