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

Josiah Carlson josiah.carlson at sbcglobal.net
Sat Jul 7 13:40:54 EDT 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



More information about the Python-list mailing list