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

Discussion in 'Python' started by Edward Diener, Jul 7, 2007.

  1. 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.
    Edward Diener, Jul 7, 2007
    #1
    1. Advertising

  2. 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
    Josiah Carlson, Jul 7, 2007
    #2
    1. Advertising

  3. 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.
    Edward Diener, Jul 7, 2007
    #3
  4. On 2007-07-07, Edward Diener <> 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!!
    Grant Edwards, Jul 7, 2007
    #4
  5. Edward Diener

    Neil Hodgson Guest

    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
    Neil Hodgson, Jul 8, 2007
    #5
  6. 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.
    Edward Diener, Jul 8, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob Swerdlow
    Replies:
    1
    Views:
    529
    Neil Hodgson
    Jul 19, 2005
  2. Tony Nelson
    Replies:
    4
    Views:
    359
    Tony Nelson
    Nov 18, 2005
  3. Kevin F

    where is os.path.expanduser?

    Kevin F, Mar 23, 2006, in forum: Python
    Replies:
    2
    Views:
    681
    Kevin F
    Mar 23, 2006
  4. Kevin F
    Replies:
    0
    Views:
    286
    Kevin F
    Mar 23, 2006
  5. Chad Perrin
    Replies:
    7
    Views:
    297
    OscarRyz
    Aug 11, 2012
Loading...

Share This Page