Could you explain a little more clearly what the problem is?  In the 
implementation of expanduser in Python 2.3.4, it uses the value of HOME env var 
if it exists, otherwise, it uses HOMEDRIVE + HOMEPATH.  I don't have access to 
a Win 2K machine right now, but my recollection is that when I did, HOMEDRIVE 
and HOMEPATH were set by Windows, so the right thing should happen.

Do you not have HOMEDRIVE + HOMEPATH set in your environment, e.g. if you open 
a CMD.exe window?

When you say that it returned %USERPROFILE%, do you mean that it returned that 
actual string, or the value of that env var?  

Or is it that you want it to return the path to your My Documents directory, 
which is not necessarily the same thing as %HOME%?

Thanks, Alec

> Hi all, I'm trying to write a multiplatform function that tries to
> return the actual user home directory. I saw that
> os.path.expanduser("~") works on Linux but on Windows2000 (at least on
> the win I used) it returns %USERPROFILE%, so I tried os.environ["HOME"]
> and it gave me the same results. So I ended up with
> os.environ["USERPROFILE"], it doesn't work on Linux but (at least) on
> Windows2000 it returns the correct information
> 
> I googled a little bit and it seems that there is no general solution,
> so I tried to merge what I found, and I wrote this little function:
> 
> def getHomeDir():
>     ''' Try to find user's home directory, otherwise return current 
> directory.'''
>     try:
>         path1=os.path.expanduser("~")
>     except:
>         path1=""
>     try:
>         path2=os.environ["HOME"]
>     except:
>         path2=""
>     try:
>         path3=os.environ["USERPROFILE"]
>     except:
>         path3=""
> 
>     if not os.path.exists(path1):
>         if not os.path.exists(path2):
>             if not os.path.exists(path3):
>                 return os.getcwd()
>             else: return path3
>         else: return path2
>     else: return path1


-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to