Finding user's home dir
Steve Holden
steve at holdenweb.com
Wed Feb 2 16:04:17 EST 2005
Nemesis wrote:
> 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
>
> Please, could you test it on your systems and tell me what you got?
> I'd like to know what it returns on different operating systems because
> I'm developing a multiplatform software.
>
> Thank you all.
It works on Cygwin too!
regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.python.org/pycon/2005/
Steve Holden http://www.holdenweb.com/
More information about the Python-list
mailing list