Getting the home directory in Python and a bug in os.path.expanduser

E

Edward Diener

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.
 
J

Josiah Carlson

Edward said:
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. 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 .

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.

- Josiah
 
E

Edward Diener

Josiah said:
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.
 
N

Neil Hodgson

Edward Diener:
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.

Look at SHGetFolderPath(CSIDL_PROFILE, ...) for Windows Me/2000 or
later. CSIDL_APPDATA is probably a better idea than CSIDL_PROFILE (its
equivalent to "%USERPROFILE%\Application Data" and roams) but
%USERPROFILE% (CSIDL_PROFILE) is compatible with previous Python behaviour.
http://msdn2.microsoft.com/en-us/library/ms647764.aspx

Neil
 
E

Edward Diener

Neil said:
Edward Diener:


Look at SHGetFolderPath(CSIDL_PROFILE, ...) for Windows Me/2000 or
later. CSIDL_APPDATA is probably a better idea than CSIDL_PROFILE (its
equivalent to "%USERPROFILE%\Application Data" and roams) but
%USERPROFILE% (CSIDL_PROFILE) is compatible with previous Python behaviour.
http://msdn2.microsoft.com/en-us/library/ms647764.aspx

Thanks, that looks good. For Windows Vista one should evidently use
SHGetKnownFolderPath(FOLDERID_Profile,...) instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top