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

Search Discussions

  • Josiah Carlson at Jul 7, 2007 at 5:40 pm

    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
  • Edward Diener at Jul 7, 2007 at 7:25 pm

    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.
  • Grant Edwards at Jul 7, 2007 at 7:35 pm

    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!!
  • Neil Hodgson at Jul 7, 2007 at 11:45 pm

    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
  • Edward Diener at Jul 8, 2007 at 2:30 am

    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJul 7, '07 at 4:28a
activeJul 8, '07 at 2:30a
posts6
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase