Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Getting the home directory in Python and a bug in os.path.expanduser

Reply
Thread Tools

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

 
 
Edward Diener
Guest
Posts: n/a
 
      07-07-2007
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.
 
Reply With Quote
 
 
 
 
Josiah Carlson
Guest
Posts: n/a
 
      07-07-2007
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. 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
 
Reply With Quote
 
 
 
 
Edward Diener
Guest
Posts: n/a
 
      07-07-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.
 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      07-07-2007
On 2007-07-07, Edward Diener <(E-Mail Removed)> wrote:

> Thank you for responding. I hope Python will fix this problem.


Python: the language that fixes itself!

--
Grant Edwards grante Yow! I'm a GENIUS! I want
at to dispute sentence
visi.com structure with SUSAN
SONTAG!!
 
Reply With Quote
 
Neil Hodgson
Guest
Posts: n/a
 
      07-07-2007
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
 
Reply With Quote
 
Edward Diener
Guest
Posts: n/a
 
      07-08-2007
Neil Hodgson wrote:
> 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


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
getting the user's home directory path Chad Perrin Ruby 7 08-11-2012 01:00 AM
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
*bug* *bug* *bug* David Raleigh Arnold Firefox 12 04-02-2007 03:13 AM
getting user's home directory on windows K.S.Sreeram Python 0 07-18-2006 03:30 PM
Directory structure of home directory? john HTML 4 06-15-2006 06:50 PM



Advertisments