Finding user's home dir

N

Nemesis

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

Mark Nenadov

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.

I tried your function in my environment (Python 2.3.3 on Linux) and it
returned the home directory properly

- -
Mark Nenadov
Python Byte Solutions
http://www.pythonbyte.com/
 
T

Timothy Grant

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.
--
Unauthorized amphibians will be toad away.

|\ | |HomePage : http://nem01.altervista.org
| \|emesis |XPN (my nr): http://xpn.altervista.org

Works beautifully on my PowerBook running Mac OSX 10.3.7

/Users/timothygrant
 
S

Steve Holden

Nemesis said:
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
 
N

Nick

Python 2.4 (#1, Jan 1 2005, 21:33:55)
[GCC 3.3.4] on linux2
Slackware 10 Current

Works! Nice.
 
K

Kartic

Nemesis said the following on 2/2/2005 2:26 PM:
Hi all, I'm trying to write a multiplatform function that tries to

def getHomeDir():
''' Try to find user's home directory, otherwise return current directory.'''

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.


Neat!

Works fine on Freebsd 5.3, Python 2.4

Thanks,
-Kartic
 
M

Miki Tebeka

Hello Nemesis,
Hi all, I'm trying to write a multiplatform function that tries to
return the actual user home directory.
...
What's wrong with:
from user import home
which does about what your code does.

Bye.
 
M

Marc Christiansen

Miki Tebeka said:
Hello Nemesis,

What's wrong with:
from user import home
which does about what your code does.

Except it also execfile()s $HOME/.pythonrc.py, which might not be wanted.

Saluton
Marc
 
P

Peter Hansen

Miki said:
What's wrong with:
from user import home
which does about what your code does.

:)

I suspect he simply didn't know about it. I didn't either...

Nemesis, please use the above recipe instead, as it makes
the more reasonable (IMHO) choice of checking for a HOME
environment variable before trying the expanduser("~")
approach. This covers folks like me who, though stuck
using Windows, despise the ridiculous Microsoft convention
of "user folders" named like "C:\Documents and Settings\Peter"
and prefer to create sensible folder like c:\users\peter
and set a HOME variable to point to it. Your approach
ignores our HOME variable.

c:\>python
Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32c:\users\peter

Yay! :)

-Peter
 
L

Lars

Works great with Python 2.3.4 on...
dare I say it...
windows xp
'C:\\Documents and Settings\\Lars'

Regards
Lars
 
B

Bernhard Herzog

Peter Hansen said:
:)

I suspect he simply didn't know about it. I didn't either...

The purpose of the user module is executing ~/.pythonrc.py, which may
not desirable. It definitely shouldn't be done by a library, for
instance. Also, that the user's home directory is available as
user.home is not documented, and I for one wouldn't want to rely on
that.

Bernhard
 
P

Peter Hansen

Bernhard said:
The purpose of the user module is executing ~/.pythonrc.py, which may
not desirable. It definitely shouldn't be done by a library, for
instance. Also, that the user's home directory is available as
user.home is not documented, and I for one wouldn't want to rely on
that.

Then please interpret my "please use the above recipe"
as suggesting he should do what the equivalent code in
the user.py module does in terms of finding the "home folder"
of the user, rather than his own home-grown approach.

-Peter
 
D

Duncan Booth

Peter said:
Nemesis, please use the above recipe instead, as it makes
the more reasonable (IMHO) choice of checking for a HOME
environment variable before trying the expanduser("~")
approach. This covers folks like me who, though stuck
using Windows, despise the ridiculous Microsoft convention
of "user folders" named like "C:\Documents and Settings\Peter"
and prefer to create sensible folder like c:\users\peter
and set a HOME variable to point to it. Your approach
ignores our HOME variable.

You could just tell Windows to move your home directory location to
c:\users\peter and then all the environment variables will be set for you
automatically.

It probably varies wildly between Windows versions, but to assign a home
folder for a local user on XP go to Control Panel/Administrative
Tools/Computer Management/Local Users and Groups/Users, select your
account, right menu Properties, Profile tab, and edit the 'Home folder'
setting. Press F1 while editing the home folder location and see that at
least someone at Microsoft thinks c:\users\... is a sensible location.

See also http://support.microsoft.com/?kbid=816313 for domain users etc.
 
M

Michael

My own, less than perfect, way of finding the users home directory is
like this:

def personal_directory ( default = None ):
pdir = None
if sys.platform.startswith ( 'win' ):
import _winreg
reg = _winreg.ConnectRegistry ( None, _winreg.HKEY_CURRENT_USER )
pkey = _winreg.OpenKey ( reg,
r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" )
pdir = _winreg.QueryValueEx ( pkey, 'Personal' )[0]
_winreg.CloseKey ( pkey )
_winreg.CloseKey ( reg )
else:
pdir = os.getenv ( 'HOME' )
if not pdir:
pdir = default
return pdir
 
N

Nemesis

Mentre io pensavo ad una intro simpatica "Miki Tebeka" scriveva:
What's wrong with:
from user import home
which does about what your code does.

On my Win2000 box it returns "%USERPROFILE%". That's no surprise because
if you look at the code it try to use os.environ["HOME"] (as
os.path.expanduser() does). And on my Win2000 system this information
points to "%USERPROFILE%" field (I don't know why, I'm not the
administrator of that machine).
 
N

Nemesis

Mentre io pensavo ad una intro simpatica "Peter Hansen" scriveva:
:)

I suspect he simply didn't know about it. I didn't either...

That's true :-D
But as I said in the other post os.environ["HOME"] doesn't work on my
Win2000 box.
Nemesis, please use the above recipe instead, as it makes
the more reasonable (IMHO) choice of checking for a HOME
environment variable before trying the expanduser("~")
approach. This covers folks like me who, though stuck
using Windows, despise the ridiculous Microsoft convention
of "user folders" named like "C:\Documents and Settings\Peter"
and prefer to create sensible folder like c:\users\peter
and set a HOME variable to point to it. Your approach
ignores our HOME variable.

If you look at ntpath.py (I think this is the 'path' module on
Windows 2000) in you Lib dir you'll see that expanduser does try
os.environ["HOME"]
So I'm not ignoring it, maybe it is redundant in my function.
The problem is that expanduser and user.home doesn't test if the value
returned is really a directory.
c:\>python
Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32c:\users\peter

Yay! :)

on my box it returns "%USERPROFILE%" ;-)
 
P

Peter Hansen

Nemesis said:
On my Win2000 box it returns "%USERPROFILE%". That's no surprise because
if you look at the code it try to use os.environ["HOME"] (as
os.path.expanduser() does). And on my Win2000 system this information
points to "%USERPROFILE%" field (I don't know why, I'm not the
administrator of that machine).

It looks a lot like somebody who didn't test things out
properly (the real administrator, not you), was trying
to do the equivalent of "set home=%userprofile%" but
managed to fail... if you do that at the command line
or in a .CMD or .BAT file, it will expand the reference
out and you'd have a "HOME=C:\Documents and Settings\whatever"
just like you'd expect.

Probably they put that in a dialog box somewhere without
realizing it wouldn't be evaluated. (At least, I don't
think environment variable substitution generally or
ever has "lazy evaluation" semantics...)

Now that I know how to do what Duncan described (thanks
Duncan!), either approach works for me. Of course,
whether I'm a likely eventual user of your particular
program is an entirely different question. ;-)

-Peter
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top